Question 861
What is printed?
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 1; i <= 6; i++) {
list.add(i * i);
}
// list = [1, 4, 9, 16, 25, 36]
for (int i = list.size() - 1; i >= 1; i -= 2) {
list.set(i, list.get(i) - list.get(i - 1));
}
System.out.println(list);Main Solution
The list is first populated with squares of 1 through 6:
list = [1, 4, 9, 16, 25, 36] (indices 0–5)
The second loop runs with i starting at list.size() - 1 = 5, decrementing by 2 each iteration, and stopping when i < 1. So i takes values: 5, 3, 1.
For each iteration, the operation is: list.set(i, list.get(i) - list.get(i - 1))
- i = 5: list.set(5, list.get(5) - list.get(4)) = list.set(5, 36 - 25) = list.set(5, 11)
list = [1, 4, 9, 16, 25, 11] - i = 3: list.set(3, list.get(3) - list.get(2)) = list.set(3, 16 - 9) = list.set(3, 7)
list = [1, 4, 9, 7, 25, 11] - i = 1: list.set(1, list.get(1) - list.get(0)) = list.set(1, 4 - 1) = list.set(1, 3)
list = [1, 3, 9, 7, 25, 11]
Final list: [1, 3, 9, 7, 25, 11], which matches option A.
Teacher Explanation
This question tests students' ability to trace through an ArrayList manipulation loop that iterates backwards with a step of -2, modifying only odd-indexed elements. The key insight is that the loop modifies indices 5, 3, and 1 (in that order), and each modification uses the already-updated list values — but since we go from high to low indices, the values at i-1 (even indices) are never modified and remain as the original squares.
Education Coach Advice
When tracing loops that modify a list in-place, always track the current state of the list after each iteration. Write out the list after each set() call. For loops that decrement by 2, carefully list all values of i before starting the trace: here 5 → 3 → 1 → (0 fails condition i >= 1, so stop). Don't forget the last iteration!
Traps
Option B [1, 4, 9, 7, 25, 11] is the most common trap: students may miss the final iteration where i = 1 and forget to update index 1 (changing 4 to 3). They correctly compute indices 5 and 3 but stop the loop trace early.
Option D [1, 3, 5, 7, 11, 11] is another trap for students who think the loop modifies all elements or uses cumulative differences incorrectly.
Answer images

