AP CSA Practice Test: String Methods
AP CSA Practice Test: String Methods
Unit 1: Using Objects and Methods | 10 Questions | AP Computer Science A
String s = "computer"; System.out.println(s.substring(3, 6));
Explanation: substring(3, 6) returns characters at index 3, 4, and 5 (end index is exclusive). "computer": c(0) o(1) m(2) p(3) u(4) t(5) e(6) r(7). Indices 3-5 = "put".
Common Mistake: Including the character at index 6. The end index in substring is always exclusive.
String s = "Java"; s.toUpperCase(); System.out.println(s);
Explanation: Strings are immutable. toUpperCase() returns a NEW String but does NOT modify the original. Since the return value is not stored, s remains "Java".
Common Mistake: Thinking toUpperCase() modifies the original String. You must reassign: s = s.toUpperCase().
I.
"apple".compareTo("banana") < 0II.
"banana".compareTo("banana") == 0III.
"Cat".equals("cat")
Explanation: I: "apple" comes before "banana" alphabetically, so compareTo returns a negative value. True. II: identical strings return 0. True. III: equals() is case-sensitive. "Cat" is not equal to "cat". False.
Common Mistake: Thinking equals() is case-insensitive. Use equalsIgnoreCase() for case-insensitive comparison.
result?String s = "programming";
int result = s.indexOf("mm");
Explanation: "programming": p(0) r(1) o(2) g(3) r(4) a(5) m(6) m(7) i(8) n(9) g(10). "mm" starts at index 6. indexOf() returns the index where the substring STARTS.
Common Mistake: Returning 7 (the second m) instead of 6 (where the substring begins).
String a = "Hello";
String b = "Hello";
String c = new String("Hello");
System.out.println((a == b) + " " + (a == c));
Explanation: a and b are String literals and share the same reference in the String pool, so a == b is true. c uses new, creating a separate object. a == c is false because they are different objects. Use .equals() to compare content.
Common Mistake: Thinking == compares content. For Strings, == compares references, not values.
String s = "abcdef";
System.out.println(s.substring(s.indexOf("cd")));
Explanation: s.indexOf("cd") returns 2. s.substring(2) returns everything from index 2 to the end: "cdef".
Common Mistake: Confusing substring with one parameter (to end) vs two parameters (specific range).
Line 1: String s = "Hello"; Line 2: String t = s.substring(0, 5); Line 3: String u = s.substring(5); Line 4: String v = s.substring(6);
Explanation: "Hello" has length 5 (indices 0-4). substring(0,5) is valid (returns "Hello"). substring(5) is valid (returns empty string ""). substring(6) throws StringIndexOutOfBoundsException because index 6 exceeds the length.
Common Mistake: Thinking substring(5) is an error. It returns an empty string, which is valid.
String s = " ab cd "; System.out.println(s.trim().length());
Explanation: trim() removes leading and trailing whitespace only. " ab cd " becomes "ab cd" (5 characters). The internal space between "ab" and "cd" is preserved.
Common Mistake: Thinking trim() removes all spaces including internal ones.
mystery("racecar")?public static boolean mystery(String s) {
int len = s.length();
for (int i = 0; i < len / 2; i++) {
if (!s.substring(i, i+1).equals(
s.substring(len-1-i, len-i)))
return false;
}
return true;
}
Explanation: This method checks if a string is a palindrome by comparing the first character with the last, second with second-to-last, etc. "racecar" reversed is "racecar", so it returns true.
Common Mistake: Miscounting the loop bounds. len/2 stops at the middle, which is correct for palindrome checking.
count after this code runs?String s = "Mississippi";
int count = 0;
int idx = s.indexOf("ss");
while (idx != -1) {
count++;
idx = s.indexOf("ss", idx + 1);
}
System.out.println(count);
Explanation: First indexOf("ss") finds "ss" at index 2. Next search from index 3: finds "ss" at index 5. Next search from index 6: no more "ss" found (-1). Count = 2.
Common Mistake: Expecting 3 by overlapping matches. indexOf("ss", idx+1) starts at idx+1, not idx+2, but "ss" only appears at indices 2 and 5.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]