ArrayList Methods

AP Computer Science A· difficulty 3/5

ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
System.out.println(list.get(2));

What is the result?

  • A

    IndexOutOfBoundsException

    check_circle
  • B

    null

  • C

    0

  • D

    Compile-time error

Explanation

Valid indices are 0 and 1 only. Accessing index 2 throws IndexOutOfBoundsException at runtime.

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

Related questions