ArrayList Trace Problems - Predict the Output (AP CSA)

Unit 4 — Exam Practice

ArrayList Trace Problems — Predict the Output

Track every add, remove, and set call. The shifting indices are where students lose points.

AP CSA Exam: May 15, 2026. ArrayList trace questions appear on every exam. Master the index shifts.
Key skill: After every remove(), all elements to the right shift left by 1. After every add(index, element), all elements at that index and beyond shift right by 1. Write the list state after each line.

Problem 1

Add and Remove Sequence

ArrayList list = new ArrayList();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.remove(1);
list.add(1, "X");
System.out.println(list);
Answer: [A, X, C, D]
After adds: [A, B, C, D]. Remove index 1 (B): [A, C, D]. Add X at index 1: [A, X, C, D].

Problem 2

Remove in Forward Loop (Bug)

ArrayList nums = new ArrayList();
nums.add(1); nums.add(2); nums.add(3); nums.add(4); nums.add(5);
for (int i = 0; i < nums.size(); i++)
{
    if (nums.get(i) % 2 == 0)
    {
        nums.remove(i);
    }
}
System.out.println(nums);
Answer: [1, 3, 5]
Remove index 1 (value 2): list becomes [1, 3, 4, 5], i becomes 2. At i=2, value is 4 (even), remove: [1, 3, 5], i becomes 3, size is 3, loop ends. Both even numbers happen to be removed here, but this pattern can skip elements in other cases.

Problem 3

set() Does Not Shift

ArrayList colors = new ArrayList();
colors.add("red"); colors.add("green"); colors.add("blue");
colors.set(1, "yellow");
System.out.println(colors.size() + " " + colors.get(1));
Answer: 3 yellow
set(1, "yellow") replaces index 1 in place. Size stays 3. No shifting occurs — this is different from add(1, "yellow") which would shift and grow the list to size 4.

Problem 4

Integer remove() Ambiguity

ArrayList data = new ArrayList();
data.add(10); data.add(20); data.add(30);
data.remove(1);
System.out.println(data);
Answer: [10, 30]
remove(1) removes the element at index 1 (value 20), not the value 1. For ArrayList, the int parameter matches the index overload.

Problem 5

Backward Removal Trace

ArrayList items = new ArrayList();
items.add("cat"); items.add("dog"); items.add("cat"); items.add("bird"); items.add("cat");
for (int i = items.size() - 1; i >= 0; i--)
{
    if (items.get(i).equals("cat"))
    {
        items.remove(i);
    }
}
System.out.println(items);
Answer: [dog, bird]
Backward traversal safely removes all occurrences of “cat” at indices 4, 2, and 0 without skipping any elements.

Need More ArrayList Practice?

The Cram Kit includes targeted ArrayList drills with step-by-step trace walkthroughs.

Get the Cram Kit — $29.99 1-on-1 Tutoring

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]