AP CSA Practice Test: String Methods

AP CSA Practice Test: String Methods

Unit 1: Using Objects and Methods | 10 Questions | AP Computer Science A

Question 1
What is the output?
String s = "computer";
System.out.println(s.substring(3, 6));
A"put"
B"pute"
C"mpu"
D"mput"

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.

Question 2
What is the output?
String s = "Java";
s.toUpperCase();
System.out.println(s);
A"JAVA"
B"Java"
C"java"
DCompile error

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().

Question 3
Consider the following. Which statements are true?

I. "apple".compareTo("banana") < 0
II. "banana".compareTo("banana") == 0
III. "Cat".equals("cat")
AI and II only
BII and III only
CI, II, and III
DI only

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.

Question 4
What is the value of result?
String s = "programming";
int result = s.indexOf("mm");
A5
B6
C7
D-1

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).

Question 5
What does the following code print?
String a = "Hello";
String b = "Hello";
String c = new String("Hello");
System.out.println((a == b) + " " + (a == c));
A"true true"
B"false false"
C"true false"
D"false true"

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.

Question 6
What is the output?
String s = "abcdef";
System.out.println(s.substring(s.indexOf("cd")));
A"cdef"
B"cd"
C"def"
D"abcd"

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).

Question 7
Which line causes a runtime error?
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);
ALine 2
BLine 3
CLine 4
DNo error occurs

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.

Question 8
What is the output?
String s = " ab cd ";
System.out.println(s.trim().length());
A4
B5
C7
D6

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.

Question 9
What does this method return when called with 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;
}
Atrue
Bfalse
CCompile error
DRuntime error

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.

Question 10
What is the value of 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);
A1
B2
C3
D4

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.

0% 0/10

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.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]