ArrayList Methods

AP Computer Science A· difficulty 2/5

ArrayList<String> list = new ArrayList<>();
list.add("x");
list.add("y");
list.add("z");
System.out.println(list.get(list.size() - 1));

What is printed?

  • A

    Index out of bounds

  • B

    x

  • C

    y

  • D

    z

    check_circle

Explanation

list.size() is 3, so list.size() - 1 = 2, which is the last index. The element is "z".

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

Related questions