AP® Computer Science A Using Objects and Methods Practice Questions
Work through AP Computer Science A practice questions on Using Objects and Methods, then check each answer against a worked solution.
Free · no sign-up Open questions need no account. Sign up or log in to open the rest.
Subtopic: Using Objects Clear filter
Question 342
Question 343
Question 344
Question 345
What is printed?
String s = "abcabc";
int first = s.indexOf("bc");
String t = s.substring(first + 2);
int second = t.indexOf("bc");
System.out.println(first + " " + second + " " + (first + second + 2));Main Solution
Step-by-step trace:
s = "abcabc"— indices: a=0, b=1, c=2, a=3, b=4, c=5first = s.indexOf("bc")— searches for"bc"in"abcabc". First occurrence starts at index 1. Sofirst = 1.t = s.substring(first + 2)=s.substring(3)="abc"second = t.indexOf("bc")— searches for"bc"in"abc". First occurrence starts at index 1. Sosecond = 1.System.out.println(first + " " + second + " " + (first + second + 2))
=1 + " " + 1 + " " + (1 + 1 + 2)
="1 1 4"
The output is 1 1 4, which matches option B.
Teacher Explanation
This question tests students' understanding of String.indexOf() and String.substring() in Java. Key points: indexOf returns the 0-based index of the first character of the found substring; substring(n) returns the string from index n to the end. Students must also recognize that the last argument in println uses parentheses, so first + second + 2 is evaluated as integer addition (giving 4), not string concatenation.
Education Coach Advice
AP Exam Strategy: When tracing String methods, write out the string with index numbers above each character. Always double-check which string object indexOf is being called on — here second is found in t, not s. Also note that parentheses around the last expression force integer arithmetic, not string concatenation.
Traps
Common traps:
- Option A (1 4 7): A student might incorrectly search for
"bc"in the original stringsagain forsecond(finding index 4), forgetting thattis a shorter substring starting at index 3. - Option C (1 3 6): A student might compute
t = s.substring(first)instead ofs.substring(first + 2), givingt = "bcabc", and thent.indexOf("bc") = 3. - Option D (0 1 3): A student might think
indexOfis 1-based or misidentify the first occurrence of"bc".
Answer images
Question 346
Consider the following code segment.
String s = "EXAMINATION";
int k = s.indexOf("NATION");
String t = s.substring(0, k) + s.substring(k + 3);
System.out.println(t + " " + t.length());What is printed as a result of executing the code segment?
Main Solution
Step-by-step trace:
s = "EXAMINATION"— indices 0–10:E(0) X(1) A(2) M(3) I(4) N(5) A(6) T(7) I(8) O(9) N(10)k = s.indexOf("NATION")— search for"NATION"in"EXAMINATION".
Starting at index 5:N(5)A(6)T(7)I(8)O(9)N(10)— yes,"NATION"starts at index 5. Sok = 5.s.substring(0, k)=s.substring(0, 5)="EXAMI"(characters at indices 0–4).s.substring(k + 3)=s.substring(8)= characters from index 8 to end ="ION".t = "EXAMI" + "ION"="EXAMIION".t.length()= 8.- Output:
EXAMIION 8.
The correct answer is D.
Teacher Explanation
indexOf("NATION") returns 5 because "NATION" begins at index 5 in "EXAMINATION". substring(0, 5) gives "EXAMI" and substring(8) gives "ION", concatenating to "EXAMIION" (length 8). Students must carefully track that k+3 = 8, not 7 or 6.
Education Coach Advice
AP Exam Strategy: For String manipulation questions, always write out the string with explicit index numbers (0, 1, 2, …) before evaluating indexOf, substring, or length. Double-check substring(a, b) includes index a but excludes index b. Verify k + 3 arithmetic carefully — one digit off changes the answer entirely.
Traps
Common traps:
- Option A (EXAMNON 7): Incorrectly computes
indexOfor uses wrong substring bounds. - Option B (EXAMIN 8): May result from using
substring(k+1)instead ofsubstring(k+3), giving"EXAMI" + "ATION"— but that's 10 chars; or other off-by-one errors. - Option C (EXAION 7): Off-by-one on the first substring, e.g., using
substring(0, 4)="EXAM"and wrong second part. - The most tempting trap is miscounting where
"NATION"starts (confusing index 4 vs. 5) or adding 3 incorrectly.
Answer images
Question 347
What is printed?
String s = "ABCDEFGHIJ";
int m = s.length() / 2;
String left = s.substring(0, m);
String right = s.substring(m);
String result = right + left;
System.out.println(result.substring(3, 7));Main Solution
Step-by-step trace:
s = "ABCDEFGHIJ"(length = 10)m = 10 / 2 = 5left = s.substring(0, 5) = "ABCDE"right = s.substring(5) = "FGHIJ"result = right + left = "FGHIJABCDE"result.substring(3, 7)extracts characters at indices 3, 4, 5, 6 (inclusive start, exclusive end):
Index 0='F', 1='G', 2='H', 3='I', 4='J', 5='A', 6='B', 7='C'
So indices 3–6 = "IJAB"
The answer is D: IJAB.
Teacher Explanation
This question tests understanding of Java's String.substring() method (0-indexed, end-exclusive), integer division, and string concatenation. Students must correctly split the string at the midpoint, concatenate in the reversed order (right + left), and then apply a second substring on the resulting string.
Education Coach Advice
AP Exam Strategy: When tracing string manipulation code, write out each variable's value explicitly after each assignment. Pay special attention to the order of operands in concatenation (right + left vs. left + right) and remember that substring(a, b) includes index a but excludes index b. Number the characters of the final string starting from 0 to avoid off-by-one errors.
Traps
Common traps:
- Option A (GHIJ): Students may forget that
resultisright + left(notleft + right) and apply substring to the original string orleft + right. - Option B (FGHI): Students may apply
substring(3, 7)directly toright("FGHIJ"), getting indices 3–6 of "FGHIJ" = "HIJ" — or confuse the concatenation order. - Option C (ABIJ): Students may mix up which half is
leftvs.rightor miscount the midpoint.
Answer images
Question 348
What is printed?
String w1 = "banana";
String w2 = "band";
String w3 = "ban";
int r1 = w1.compareTo(w2);
int r2 = w2.compareTo(w3);
System.out.println((r1 > 0) + " " + (r2 < 0) + " " + (r1 + r2 > 0));Main Solution
Step-by-step evaluation:
r1 = w1.compareTo(w2) compares "banana" to "band".
- Both strings share the prefix
"ban". The 4th character of"banana"is'a'and the 4th character of"band"is'd'. 'a'has ASCII value 97;'d'has ASCII value 100.- So
r1 = 97 - 100 = -3(a negative value).
r2 = w2.compareTo(w3) compares "band" to "ban".
- Both share the prefix
"ban"."band"has a 4th character'd', while"ban"has no 4th character (it is shorter). - In Java,
compareToreturns the difference in lengths when one string is a prefix of the other:4 - 3 = 1(a positive value). - So
r2 = 1.
Evaluating the three boolean expressions:
(r1 > 0): Is-3 > 0? false(r2 < 0): Is1 < 0? false(r1 + r2 > 0): Is-3 + 1 > 0? Is-2 > 0? false
The printed output is: false false false
Answer: A
Teacher Explanation
String.compareTo() returns a negative integer if the calling string lexicographically precedes the argument, zero if equal, and a positive integer if it follows. "banana" precedes "band" (because 'a' < 'd' at index 3), giving a negative r1. "band" follows "ban" (because it is longer and "ban" is a prefix), giving a positive r2. Since r1 is negative and r2 is positive but |r1| > r2, their sum is still negative. All three boolean expressions evaluate to false.
Education Coach Advice
AP Exam Strategy: When evaluating compareTo, always compare character-by-character from left to right. The first position where the characters differ determines the sign of the result. If one string is a prefix of the other, the shorter string comes first (returns negative from the longer string's perspective). Never rely on string length alone to determine lexicographic order.
Traps
Common traps:
- Option D (true true true): Students may incorrectly assume
"banana" > "band"because "banana" is longer, but lexicographic comparison is character-by-character, not by length. At index 3,'a' < 'd', so"banana"actually comes before"band". - Option C (false true false): Students might think
r2 < 0because"band"is longer than"ban"and confuse the direction of comparison, butw2.compareTo(w3)returns positive (w2 follows w3), not negative. - Option B (true false false): A reversal of the r1 sign error — thinking
"banana" > "band"due to length.
Answer images
Question 349
What is printed?
String p = "Java";
String q = p.substring(1) + p.length() + p.substring(0, 1);
System.out.println(q + " " + q.indexOf("4"));Main Solution
Step-by-step trace:
p = "Java"
p.substring(1)→"ava"(characters from index 1 to end)p.length()→4(the integer 4)p.substring(0, 1)→"J"(character at index 0 only)
String concatenation is left-to-right:"ava" + 4 → "ava4" (int 4 converted to String)"ava4" + "J" → "ava4J"
So q = "ava4J".
Now evaluate q.indexOf("4"):"ava4J" — indices: a=0, v=1, a=2, 4=3, J=4q.indexOf("4") → 3
The println prints: q + " " + q.indexOf("4")
= "ava4J" + " " + 3
= ava4J 3
The correct answer is A.
Teacher Explanation
This question tests string method knowledge (substring, length, indexOf) and string concatenation with mixed types. Key insight: when an int is concatenated with a String, it is converted to its String representation. Students must also correctly apply 0-based indexing for substring and indexOf.
Education Coach Advice
On AP CSA string questions, always trace each method call individually before combining results. Remember: substring(a, b) includes index a but excludes index b; indexOf returns the 0-based position of the first match. Write out the string character-by-character with indices to avoid off-by-one errors.
Traps
Option C (ava4J 5) is the most common trap: students may count the length of q (which is 5) instead of finding the index of "4" within q. They confuse q.length() with q.indexOf("4").
Option B (Java4 5) traps students who think p.substring(1) returns the full string or who reverse the concatenation order.
Option D (4J 2) traps students who misread p.substring(1) as starting from index 2 or who drop the first part of the concatenation.
Answer images
Question 350
What is printed by the following code segment?
String s = "programming";
String a = s.substring(0, s.indexOf("gram"));
String b = s.substring(s.indexOf("m") + 1, s.indexOf("ing"));
System.out.println(a.length() + b.length() + " " + a + b);Main Solution
Step-by-step trace:
Given: s = "programming"
Index positions: p=0, r=1, o=2, g=3, r=4, a=5, m=6, m=7, i=8, n=9, g=10
Computing a:
s.indexOf("gram")finds "gram" starting at index 3.a = s.substring(0, 3)→"pro"a.length() = 3
Computing b:
s.indexOf("m")finds the first 'm' at index 6.s.indexOf("m") + 1 = 7s.indexOf("ing")finds "ing" starting at index 8.b = s.substring(7, 8)→"m"b.length() = 1
Computing the print statement:
a.length() + b.length()=3 + 1 = 4(integer addition)- Then
+ " "converts to string:"4 " - Then
+ a:"4 pro" - Then
+ b:"4 prom"
Output: 4 prom
The correct answer is C.
Teacher Explanation
This question tests students' understanding of Java's String methods: substring() and indexOf(), as well as operator precedence (integer addition before string concatenation). The key insight is that a.length() + b.length() evaluates as integer addition (4) before the string concatenation with " " begins.
Education Coach Advice
When tracing Java string problems on the AP exam: (1) Write out the index of every character in the string. (2) Evaluate indexOf() calls carefully — they return the starting index of the first occurrence. (3) Remember substring(a, b) is inclusive of a and exclusive of b. (4) Watch for integer addition vs. string concatenation — left-to-right evaluation matters!
Traps
Common traps:
- Option A (3 pro): Students may forget to compute
bcorrectly or think the integer sum is only 3 (e.g., confusingb.length()as 0). - Option B (5 proam): Students may use
s.indexOf("m")= 6 without adding 1, givingb = s.substring(6, 8) = "mm", or miscalculate the sum as 5. - Option D (3 prom): Students may correctly find
b = "m"but miscalculate the integer sum as 3 instead of 4, perhaps thinkinga.length()is 2.
Answer images
AP® and Advanced Placement® are registered trademarks of the College Board, which is not affiliated with, and does not endorse, Verityprep. Names are used for identification purposes only.

