AP Computer Science A ArrayList — Worked Answer Explanations

Unit 7 · 12 questions explained

Below is a complete answer key for our AP Computer Science A ArrayList practice questions. For each question you'll find the correct choice, a full written explanation of how to get there, and — for every wrong answer — a short note on exactly why it's tempting and where it goes wrong. Reading these straight through is one of the fastest ways to find the gaps in a unit before exam day.

Prefer to test yourself first? Take the timed ArrayList practice test and come back here to review, or head back to the ArrayList unit overview.

In-content ad
  1. Question 1 · Easy

    What is the output of the following code?

    import java.util.ArrayList;
    ArrayList<Integer> list = new ArrayList<>();
    list.add(10);
    list.add(20);
    list.add(30);
    System.out.println(list.size());
    System.out.println(list.get(1));
    
    • A
      3\n20Correct
    • B
      3\n10
      Why not B: get(1) returns the element at index 1 (zero-indexed), which is 20, not 10 (which is at index 0).
    • C
      2\n20
      Why not C: size() returns the number of elements (3), not the last valid index (2).
    • D
      3\n30
      Why not D: get(1) is index 1, which is 20. 30 is at index 2.
    Explanation

    size() returns the number of elements: 3. get(1) retrieves the element at index 1 (0-indexed): 20. Note that size() is always 1 more than the last valid index.

    Key takeaway

    `ArrayList.size()` is one more than the last valid index. Elements are 0-indexed like arrays.

  2. Question 2 · Easy

    What is the state of list after the following code executes?

    ArrayList<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add(1, "X");
    
    • A
      [A, B, X, C]
      Why not A: add(1, "X") inserts "X" AT index 1, shifting "B" and "C" right. Result is [A, X, B, C].
    • B
      [A, X, B, C]Correct
    • C
      [X, A, B, C]
      Why not C: add(1, "X") inserts at index 1 (second position), not index 0.
    • D
      [A, B, C, X]
      Why not D: add(index, value) inserts at the specified position. The no-index add appends to the end, but add(1, "X") inserts at index 1.
    Explanation

    add(int index, E element) inserts at the given position, shifting everything from that index onward to the right. After three adds: [A, B, C]. After add(1, "X"): [A, X, B, C].

    Key takeaway

    `add(index, value)` inserts at the given index; all elements at that index and beyond shift right by one.

  3. Question 3 · Easy

    What is the output of the following code?

    ArrayList<Integer> list = new ArrayList<>();
    list.add(5);
    list.add(10);
    list.add(15);
    list.remove(1);
    System.out.println(list);
    
    • A
      [5, 15]Correct
    • B
      [5, 10]
      Why not B: remove(1) removes the element at INDEX 1 (which is 10), not the last element.
    • C
      [10, 15]
      Why not C: remove(1) removes the element at index 1 (10). The remaining elements shift left: [5, 15].
    • D
      [5, 10, 15]
      Why not D: remove modifies the list. One element is removed; the list has 2 elements, not 3.
    Explanation

    remove(int index) removes the element at that index. Index 1 holds value 10. After removal: [5, 15]. The element at index 2 (15) shifts left to index 1.

    Key takeaway

    `remove(int index)` removes by position; `remove(Object)` removes by value. For Integer lists, `remove(1)` is by index.

  4. Question 4 · Easy

    What is the output of the following code?

    ArrayList<Integer> list = new ArrayList<>();
    for (int i = 1; i <= 5; i++) {
        list.add(i);
    }
    for (int i = 0; i < list.size(); i++) {
        System.out.print(list.get(i) + " ");
    }
    
    • A
      1 2 3 4 5Correct
    • B
      0 1 2 3 4
      Why not B: The first loop adds values 1 through 5, not the indices 0 through 4.
    • C
      1 2 3 4
      Why not C: The condition i < list.size() (i < 5) iterates i = 0,1,2,3,4, accessing all 5 elements.
    • D
      2 3 4 5 6
      Why not D: The first loop adds i (1–5), not i+1. The values added are exactly 1, 2, 3, 4, 5.
    Explanation

    The first loop adds integers 1 through 5 to the list. The second loop accesses indices 0 through 4 with list.get(i), printing elements 1, 2, 3, 4, 5 in order.

    Key takeaway

    Index-based ArrayList traversal: `for (int i = 0; i < list.size(); i++)` with `list.get(i)` to access elements.

  5. Question 5 · Easy

    What is the output of the following code?

    ArrayList<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(2);
    list.add(3);
    list.add(4);
    for (int i = 0; i < list.size(); i++) {
        if (list.get(i) % 2 == 0) {
            list.remove(i);
        }
    }
    System.out.println(list);
    
    • A
      [2, 4]
      Why not A: Reversed the condition — this code removes evens, not odds.
    • B
      [1, 3, 4]
      Why not B: Missed that after 2 is removed, 4 shifts to index 1 but i becomes 2; then list.get(2) is 4 and also gets removed.
    • C
      [1, 3]Correct
    • D
      [1, 2, 3]
      Why not D: Confused which element gets skipped — when an element is removed at index i and i increments, the new element at i is what gets re-checked.
    Explanation

    Trace: list=[1,2,3,4]. i=0: 1 is odd (keep). i=1: 2 is even (remove) → list=[1,3,4]; i++ → i=2. i=2: list.get(2)=4 (even, remove) → list=[1,3]; i++ → i=3. 3 >= size(2), loop ends. Result: [1, 3]. Note: 3 was skipped because after removing 2, index 1 now holds 3, but i jumped to 2.

    Key takeaway

    Removing elements during forward traversal skips elements. Fix: iterate backward, or use an iterator, or decrement i after removal.

  6. Question 6 · Easy

    What is the output of the following code?

    ArrayList<Integer> list = new ArrayList<>();
    list.add(100);
    list.add(200);
    list.set(0, 999);
    System.out.println(list.get(0));
    System.out.println(list.size());
    
    • A
      999\n2Correct
    • B
      999\n3
      Why not B: set replaces the element at the given index — it does NOT add a new element. Size remains 2.
    • C
      100\n2
      Why not C: set(0, 999) replaces the element at index 0 (previously 100) with 999. get(0) now returns 999.
    • D
      100\n3
      Why not D: set replaces, not adds. The element at index 0 is replaced, not a new element added.
    Explanation

    set(index, value) replaces the element at the given index with the new value. It does NOT change the size of the list. After set(0, 999), index 0 holds 999. size() is still 2.

    Key takeaway

    `set(index, value)` replaces an existing element. `add(value)` appends. `add(index, value)` inserts. Only add-variants increase size.

  7. Question 7 · Easy

    What is the output of the following code?

    ArrayList<String> list = new ArrayList<>();
    list.add("dog");
    list.add("cat");
    list.add("dog");
    list.add("bird");
    int count = 0;
    for (String s : list) {
        if (s.equals("dog")) count++;
    }
    System.out.println(count);
    
    • A
      1
      Why not A: "dog" appears twice in the list (at indices 0 and 2). The enhanced for loop visits all elements.
    • B
      2Correct
    • C
      3
      Why not C: Only "dog" entries are counted. "cat" and "bird" do not match. There are 2 "dog" entries.
    • D
      4
      Why not D: count only increments when s.equals("dog"). The list has 4 elements total but only 2 are "dog".
    Explanation

    The enhanced for loop visits all elements. Using .equals() for String comparison: "dog" (match, count=1), "cat" (no), "dog" (match, count=2), "bird" (no). count = 2.

    Key takeaway

    Use `.equals()` (not `==`) to compare String values in an ArrayList. The enhanced for loop visits every element.

  8. Question 8 · Easy

    Which of the following correctly removes all elements equal to 0 from an ArrayList without skipping elements?

    ArrayList<Integer> list = new ArrayList<>();
    // list contains: [0, 5, 0, 3, 0]
    
    • A
      java\nfor (int i = 0; i < list.size(); i++) {\n if (list.get(i) == 0) list.remove(i);\n}
      Why not A: Forward traversal with removal skips elements. After removing at index 0, the next element shifts to index 0, but i jumps to 1.
    • B
      java\nfor (int i = list.size() - 1; i >= 0; i--) {\n if (list.get(i) == 0) list.remove(i);\n}Correct
    • C
      java\nfor (int val : list) {\n if (val == 0) list.remove(val);\n}
      Why not C: Modifying a list while using an enhanced for loop throws ConcurrentModificationException at runtime.
    • D
      java\nlist.remove(0);
      Why not D: list.remove(0) removes only the element at index 0, not all zeros. And calling it once only removes one element.
    Explanation

    Backward traversal (B) is the safe way to remove elements by index: removing an element only shifts elements AFTER it, which have already been processed. Option C throws ConcurrentModificationException. Option A skips elements.

    Key takeaway

    To safely remove elements during traversal, iterate BACKWARD (from size-1 to 0). Or use an Iterator with remove().

  9. Question 9 · Easy

    What is the output of the following code?

    ArrayList<Double> list = new ArrayList<>();
    list.add(1.5);
    list.add(2.5);
    list.add(3.5);
    double sum = 0;
    for (double d : list) {
        sum += d;
    }
    System.out.println(sum);
    
    • A
      7.5Correct
    • B
      6.0
      Why not B: 1.5+2.5+3.5 = 7.5, not 6.0. All three values are added.
    • C
      Compile error: cannot use double in enhanced for with ArrayList
      Why not C: Java auto-unboxes Double objects to double primitives in the enhanced for loop. This is valid.
    • D
      7.0
      Why not D: 1.5+2.5+3.5 = 7.5 exactly. No truncation occurs here.
    Explanation

    ArrayList<Double> stores Double objects. The enhanced for loop with double d automatically unboxes each Double to a double. Sum = 1.5 + 2.5 + 3.5 = 7.5.

    Key takeaway

    Java auto-unboxes wrapper types (Double, Integer) to primitives when assigned to a primitive variable in an enhanced for loop.

  10. Question 10 · Medium

    What is the output of the following code?

    ArrayList<Integer> list = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        list.add(i * 2);
    }
    list.remove(Integer.valueOf(4));
    System.out.println(list);
    
    • A
      [0, 2, 6, 8]Correct
    • B
      [0, 2, 4, 6, 8]
      Why not B: remove(Integer.valueOf(4)) calls the Object-based remove, which removes the FIRST occurrence of value 4. List becomes [0, 2, 6, 8].
    • C
      [0, 2, 4, 8]
      Why not C: remove(Integer.valueOf(4)) removes the element with VALUE 4 (at index 2), not the element at index 4. Element at index 4 is 8.
    • D
      [0, 2, 4, 6]
      Why not D: remove(Integer.valueOf(4)) removes value 4, not the last element. 8 remains in the list.
    Explanation

    The list after the loop: [0, 2, 4, 6, 8]. remove(Integer.valueOf(4)) calls the remove(Object) overload, which removes the first element EQUAL TO 4 (value 4 at index 2). Result: [0, 2, 6, 8].

    Key takeaway

    For `ArrayList<Integer>`, `remove(int)` removes by index; `remove(Integer.valueOf(n))` removes by value. Know which overload is called.

  11. Question 11 · Medium

    What is the output of the following code?

    ArrayList<String> list = new ArrayList<>();
    list.add("apple");
    list.add("banana");
    list.add("cherry");
    String result = "";
    for (int i = list.size() - 1; i >= 0; i--) {
        result += list.get(i).charAt(0);
    }
    System.out.println(result);
    
    • A
      abc
      Why not A: The loop iterates backward (from index 2 to 0). First charAt(0) = 'c' (cherry), then 'b' (banana), then 'a' (apple).
    • B
      cbaCorrect
    • C
      cherry banana apple
      Why not C: charAt(0) returns only the first character of each word, not the whole word.
    • D
      cab
      Why not D: The loop goes backward: index 2 (cherry→'c'), index 1 (banana→'b'), index 0 (apple→'a'). Order is c, b, a.
    Explanation

    Backward traversal: i=2 → list.get(2)="cherry" → charAt(0)='c'. i=1 → "banana" → 'b'. i=0 → "apple" → 'a'. result = "cba".

    Key takeaway

    Backward traversal processes elements from last to first. Combine with charAt(0) to extract first characters in reverse order.

  12. Question 12 · Medium

    What is the output of the following code?

    ArrayList<Integer> a = new ArrayList<>();
    a.add(1); a.add(2); a.add(3);
    ArrayList<Integer> b = new ArrayList<>(a);
    b.add(4);
    System.out.println(a.size());
    System.out.println(b.size());
    
    • A
      4\n4
      Why not A: new ArrayList<>(a) creates a NEW ArrayList copying a's elements. Adding to b does NOT affect a. a.size() = 3.
    • B
      3\n4Correct
    • C
      3\n3
      Why not C: b starts with 3 elements (copied from a), then b.add(4) adds one more. b.size() = 4.
    • D
      4\n3
      Why not D: new ArrayList<>(a) creates an independent copy. Modifying b does not affect a.
    Explanation

    new ArrayList<>(a) creates a copy of the list — a new ArrayList containing the same elements, but it's a separate object. Modifications to b don't affect a. a has 3 elements; b gets a copy of those 3 plus adds 4, so b has 4.

    Key takeaway

    `new ArrayList<>(otherList)` creates an independent copy. Unlike `b = a` (same reference), modifications to the copy don't affect the original.