Traversing ArrayLists

AP Computer Science A· difficulty 3/5

import java.util.ArrayList;
public static ArrayList<Integer> build() {
    ArrayList<Integer> a = new ArrayList<>();
    a.add(1); a.add(2); a.add(3);
    a.add(1, 5);
    a.remove(2);
    a.set(0, 9);
    return a;
}
// Call: System.out.println(build());

What is printed?

  • A

    [9, 5, 3]

    check_circle
  • B

    [9, 5, 2, 3]

  • C

    [1, 5, 3]

  • D

    [9, 1, 5, 3]

Explanation

Start [1,2,3]. add(1,5) -> [1,5,2,3]. remove(2) removes index 2 -> [1,5,3]. set(0,9) -> [9,5,3].

Want 10 more like this — adaptive to your weak spots?

Related questions