AP® Computer Science A Multiple Choice Questions
Work through AP Computer Science A multiple-choice questions unit by unit, 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 962
Question 963
Question 964
Question 965
Question 966
What is printed?
int product = 1;
for (int i = 1; i <= 6; i++) {
if (i % 2 == 0) {
product *= i;
}
}
System.out.println(product);Main Solution
The code initializes product = 1 and loops i from 1 to 6 (inclusive). Inside the loop, it multiplies product by i only when i % 2 == 0, i.e., only for even values of i.
The even values in the range [1, 6] are: 2, 4, 6.
Step-by-step:
i = 1: 1 % 2 = 1 ≠ 0 → skipi = 2: 2 % 2 = 0 → product = 1 × 2 = 2i = 3: 3 % 2 = 1 ≠ 0 → skipi = 4: 4 % 2 = 0 → product = 2 × 4 = 8i = 5: 5 % 2 = 1 ≠ 0 → skipi = 6: 6 % 2 = 0 → product = 8 × 6 = 48
So System.out.println(product) prints 48. The correct answer is A.
Teacher Explanation
This question tests students' ability to trace a loop with a conditional. The key insight is that only even values of i (2, 4, 6) contribute to the product. The product of the even integers from 2 to 6 is 2 × 4 × 6 = 48.
Education Coach Advice
When tracing loops on the AP exam, create a small table tracking the loop variable and any accumulator variable at each iteration. Pay close attention to the loop condition (i <= 6 vs i < 6) and any inner conditionals. This prevents off-by-one errors and missed conditions.
Traps
Option C (24) is a common trap: students may mistakenly compute 2 × 3 × 4 = 24, confusing which values pass the even check, or stopping the loop early. Option D (720) is 6! (factorial of 6), which would be the result if the condition were removed entirely and all values 1–6 were multiplied. Option B (36) might result from multiplying 6 × 6 or some other miscount.
Answer images
Question 967
What is printed?
int a = 4, b = 7, c = 10;
boolean p = !(a < b && b < c);
boolean q = !(a < b) || !(b < c);
boolean r = a >= b || b >= c;
System.out.println(p + " " + q + " " + r + " " + (p == q && q == r));Main Solution
Let's evaluate each boolean variable step by step with a = 4, b = 7, c = 10.
Evaluate p:
p = !(a < b && b < c)
a < b→4 < 7→trueb < c→7 < 10→truetrue && true→true!(true)→ false
Evaluate q:
q = !(a < b) || !(b < c)
!(a < b)→!(4 < 7)→!(true)→false!(b < c)→!(7 < 10)→!(true)→falsefalse || false→ false
Evaluate r:
r = a >= b || b >= c
a >= b→4 >= 7→falseb >= c→7 >= 10→falsefalse || false→ false
Evaluate (p == q && q == r):
p == q→false == false→trueq == r→false == false→truetrue && true→ true
So the output is: false false false true, which matches option D.
Teacher Explanation
This question tests De Morgan's Law and boolean expression evaluation. Note that p = !(a < b && b < c) is logically equivalent to !(a < b) || !(b < c) by De Morgan's Law, which is exactly q. Therefore p and q will always be equal. Since all three booleans evaluate to false, the final expression (p == q && q == r) is true.
Education Coach Advice
When evaluating boolean expressions on the AP exam, work inside-out: evaluate the innermost sub-expressions first, then apply negations and logical operators. For De Morgan's Law, remember: !(A && B) equals !A || !B, and !(A || B) equals !A && !B. Also, when you see a final expression comparing booleans with ==, substitute the computed values and evaluate carefully — don't assume the result matches the individual values.
Traps
A common trap is option A (true true true true): students might incorrectly negate the entire compound expression or misapply De Morgan's Law, thinking the negation of a true AND expression yields true. Another trap is option C (false false false false): students might forget that p == q && q == r compares the boolean values — since all three are false, the equality checks both return true, making the last value true, not false.
Answer images
Question 968
What is printed?
int sum = 0;
for (int i = 1; i <= 4; i++) {
for (int j = i; j <= 4; j++) {
sum += i * j;
}
}
System.out.println(sum);Main Solution
We need to trace the nested loops and accumulate sum.
The outer loop runs i from 1 to 4. The inner loop runs j from i to 4. Each iteration adds i * j to sum.
| i | j | i * j |
|---|---|---|
| 1 | 1 | 1 |
| 1 | 2 | 2 |
| 1 | 3 | 3 |
| 1 | 4 | 4 |
| 2 | 2 | 4 |
| 2 | 3 | 6 |
| 2 | 4 | 8 |
| 3 | 3 | 9 |
| 3 | 4 | 12 |
| 4 | 4 | 16 |
Sum = 1 + 2 + 3 + 4 + 4 + 6 + 8 + 9 + 12 + 16 = 65
Therefore, System.out.println(sum) prints 65, which is answer C.
Teacher Explanation
The key detail is that the inner loop starts at j = i, not j = 1. This means the pairs (i, j) only cover the upper triangle of the multiplication table (where j ≥ i). Students must carefully trace the starting condition of the inner loop. The total of all products in this triangular region is 65.
Education Coach Advice
When tracing nested loops on the AP exam, always note the starting condition of the inner loop — here j = i is the critical detail. Create a small table tracking i, j, and the running sum. This systematic approach prevents arithmetic errors and ensures you don't miss any iterations.
Traps
Option B (55) is a common trap: 55 is the sum of all integers from 1 to 10, which students might arrive at if they confuse this with a simple summation loop. Option A (60) might result from starting the inner loop at j = 1 but only summing i + j instead of i * j. Option D (70) could result from starting the inner loop at j = 1 instead of j = i, computing the full upper-left triangle including the diagonal twice.
Answer images
Question 969
What is printed?
String s = "aAbBcCdD";
String result = "";
for (int i = 0; i < s.length(); i++) {
String ch = s.substring(i, i + 1);
if (ch.compareTo("a") >= 0 && ch.compareTo("z") <= 0) {
result += ch;
}
}
System.out.println(result);Main Solution
The code iterates over each character of the string "aAbBcCdD" and checks whether each single-character substring ch satisfies:
ch.compareTo("a") >= 0 && ch.compareTo("z") <= 0This condition is true only for lowercase letters (characters whose Unicode/ASCII values fall between 'a' (97) and 'z' (122) inclusive).
Let's trace through each character of "aAbBcCdD":
| Index | ch | compareTo("a") >= 0 | compareTo("z") <= 0 | Added? |
|---|---|---|---|---|
| 0 | a | 0 >= 0 ✓ | -25 <= 0 ✓ | Yes |
| 1 | A | -32 >= 0 ✗ | — | No |
| 2 | b | 1 >= 0 ✓ | -24 <= 0 ✓ | Yes |
| 3 | B | -31 >= 0 ✗ | — | No |
| 4 | c | 2 >= 0 ✓ | -23 <= 0 ✓ | Yes |
| 5 | C | -30 >= 0 ✗ | — | No |
| 6 | d | 3 >= 0 ✓ | -22 <= 0 ✓ | Yes |
| 7 | D | -29 >= 0 ✗ | — | No |
Only the lowercase letters a, b, c, d are appended to result, so System.out.println(result) prints abcd.
The correct answer is C.
Teacher Explanation
This question tests students' understanding of String.compareTo() and Unicode/ASCII ordering. Uppercase letters ('A'–'Z') have ASCII values 65–90, which are less than 'a' (97), so ch.compareTo("a") returns a negative number for uppercase letters, failing the >= 0 check. Only lowercase letters pass both conditions and get appended to result.
Education Coach Advice
When you see compareTo() on single characters, remember that Java uses Unicode (ASCII) ordering: uppercase letters (65–90) come before lowercase letters (97–122). So "A".compareTo("a") returns a negative number. Trace through a few characters manually to confirm which pass the condition before selecting your answer.
Traps
Option A (ABCD): A student might mistakenly think the condition filters for uppercase letters, reversing the compareTo logic. Option B (aAbBcCdD): A student might think all characters pass the condition, not realizing uppercase letters fail compareTo("a") >= 0. Option D (AbCd): A student might confuse which letters are uppercase vs. lowercase in the original string or misread the alternating pattern.
Answer images
Question 970
What is printed?
int x = 6;
boolean a = x % 2 == 0 && x % 4 == 0;
boolean b = x % 3 == 0 || x % 4 == 0;
boolean c = !(x % 8 == 0) || (x % 2 == 0);
System.out.println(a + " " + b + " " + c);Main Solution
Let's evaluate each boolean expression with x = 6:
Evaluating a:
a = x % 2 == 0 && x % 4 == 0
x % 2 == 0→6 % 2 = 0→truex % 4 == 0→6 % 4 = 2→2 == 0→falsetrue && false→false
Evaluating b:
b = x % 3 == 0 || x % 4 == 0
x % 3 == 0→6 % 3 = 0→true- Since the left side is
true, short-circuit evaluation makesb = trueimmediately. true
Evaluating c:
c = !(x % 8 == 0) || (x % 2 == 0)
x % 8 == 0→6 % 8 = 6→6 == 0→false!(false)→true- Since the left side is
true, short-circuit evaluation makesc = trueimmediately. true
Therefore, System.out.println(a + " " + b + " " + c) prints: false true true
The correct answer is B.
Teacher Explanation
This question tests students' ability to evaluate compound boolean expressions involving the modulo operator (%), logical AND (&&), logical OR (||), and logical NOT (!). The key insight for a is that while 6 is divisible by 2, it is NOT divisible by 4, so the AND expression is false. For b, 6 is divisible by 3, making the OR true. For c, 6 is not divisible by 8, so !(false) is true, making the whole OR expression true.
Education Coach Advice
AP Exam Strategy: When evaluating compound boolean expressions, work through each sub-expression one at a time and write down intermediate results. Pay special attention to the && vs || distinction — AND requires BOTH sides to be true, while OR only requires ONE side. Also remember that ! (NOT) flips the boolean value. For modulo, recall that a % b == 0 means a is evenly divisible by b.
Traps
Common traps:
- Option A (true true true): Students may incorrectly evaluate
aastruebecause 6 is even (divisible by 2), forgetting that the AND also requires divisibility by 4, which 6 does not satisfy. - Option C (false false true): Students may incorrectly evaluate
basfalseby only checkingx % 4 == 0(which is false) and forgetting thatx % 3 == 0is true, making the OR true. - Option D (true false true): A combination of both errors above — misreading
aas true andbas false.
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.

