AP Computer Science A · Topic 2.7
String Methods Practice
Part of Using Objects.
Practice questions
37
Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.
Sample questions
5 of 37 — sign in to practice the rest with adaptive difficulty and mastery tracking.
Sample 1difficulty 1/5
What does <code>"".length()</code> return?
- A
Throws exception
- Bcheck_circle
0
- C
1
- D
null
Why
Empty string has length 0.
- A
Sample 2difficulty 1/5
<code>"hello".length()</code> returns
- A
4
- B
Compile error
- Ccheck_circle
5
- D
6
Why
Five characters.
- A
Sample 3difficulty 2/5
String s = "abcdef"; System.out.println(s.indexOf("cd"));What is printed?
- Acheck_circle
2
- B
3
- C
1
- D
-1
Why
The substring "cd" begins at index 2 in "abcdef".
- A
Sample 4difficulty 2/5
String a = "Cat"; String b = "cat"; System.out.println(a.equalsIgnoreCase(b));What is printed?
- A
1
- B
0
- Ccheck_circle
true
- D
false
Why
equalsIgnoreCase compares without regard to case, so "Cat" and "cat" are considered equal.
- A
Sample 5difficulty 2/5
String s = ""; System.out.println(s.length());What is printed?
- A
null
- B
Error
- Ccheck_circle
0
- D
1
Why
The empty string contains zero characters, so length() returns 0.
- A