ArrayList Methods

AP Computer Science A· difficulty 2/5

for (int i = 1; i <= list.size(); i++) {
    System.out.println(list.get(i));
}

What is the bug?

  • A

    ArrayList lacks a get method

  • B

    Nothing is wrong

  • C

    println cannot accept an Object

  • D

    Indices start at 0 and end at size()-1; should be i = 0; i < list.size()

    check_circle

Explanation

Java collections are zero-indexed. Starting at 1 skips the first element and going through size() throws IndexOutOfBoundsException.

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

Related questions