AP® Computer Science A Using Objects and Methods Multiple Choice Questions
Work through AP Computer Science A multiple-choice 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 252
Question 253
Question 254
Question 255
Question 256
Question 257
Question 258
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 259
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 260
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
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.

