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 = a;
b.add(3);
System.out.println(a.size());

What is printed?

  • A

    3

    check_circle
  • B

    2

  • C

    1

  • D

    4

Explanation

b and a refer to the same ArrayList object. Adding through b also affects a, so size becomes 3.

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

Related questions