while Loops

AP Computer Science A· difficulty 4/5

What does this print?

<code>int[] a = {1, 2, 3, 4, 5}; int idx = -1; for (int i = 0; i < a.length; i++) { if (a[i] == 3) { idx = i; break; } } System.out.println(idx); </code></pre>

  • A

    0

  • B

    3

  • C

    -1

  • D

    2

    check_circle

Explanation

Stops at i=2 (a[2]=3); idx = 2.

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

Related questions