Informal Code Analysis (Loop Tracing)

AP Computer Science A· difficulty 4/5

<code>for (int i = 0; i <= arr.length; i++)</code> (note <code><=</code>) on a length-5 array

  • A

    Compile error

  • B

    Works fine

  • C

    Skips one element

  • D

    Throws ArrayIndexOutOfBoundsException at i=5

    check_circle

Explanation

Valid indices are 0..length-1; <code><=</code> would access index <code>length</code> (out of bounds).

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

Related questions