Question 161
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

