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.
Question 362
Question 363
Question 364
Question 365
Question 366
What is printed?
int x = - 15, y = 4;
int z = Math.abs(x) / y;
int w = Math.abs(x) % y;
System.out.println(Math.abs(z - w) + Math.pow(w, z));Main Solution
Correct: B. Math.abs(-15) = 15. z = 15/4 = 3. w = 15%4 = 3. Math.abs(3 - 3) = 0. Math.pow(3, 3) = 27.0. Result: 0 + 27.0 = 27.0.
Teacher Explanation
Education Coach Advice
Traps
Answer images
Question 367
What is printed by the following code segment?
int a = 58;
int b = a / 10 + a % 10;
int c = b * b;
double d = Math.sqrt(c - 25);
System.out.println(b + " " + d);Main Solution
Correct: A. a / 10 = 5, a % 10 = 8, so b = 13. c = 13 * 13 = 169. Math.sqrt(169 - 25) = Math.sqrt(144) = 12.0. Output: 13 12.0.
Teacher Explanation
Education Coach Advice
Traps
Answer images
Question 368
What is printed?
Integer x = 17;
Integer y = 5;
double result = Math.pow(x % y, x / y) + 0.5;
System.out.println(result);Main Solution
Correct: D. x % y = 17 % 5 = 2. x / y = 17 / 5 = 3. Math.pow(2, 3) = 8.0. 8.0 + 0.5 = 8.5.
Teacher Explanation
Education Coach Advice
Traps
Answer images
Question 369
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 370
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
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.

