Developing Algorithms Using ArrayLists

AP Computer Science A· difficulty 3/5

ArrayList<Integer> a = new ArrayList<>();
a.add(1);
a.add(2);
ArrayList<Integer> b = new ArrayList<>();
for (int x : a) b.add(x);
b.add(99);
System.out.println(a.size() + " " + b.size());

What is printed?

  • A

    2 3

    check_circle
  • B

    1 2

  • C

    2 2

  • D

    3 3

Explanation

b is a separate ArrayList copy, so adding to b does not affect a. Sizes are 2 and 3.

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

Related questions