while Loops

AP Computer Science A· difficulty 3/5

What happens with this code?

<code>int i = 0; while (i < 5) { System.out.println("hi"); } </code></pre>

  • A

    Compile error

  • B

    Prints 'hi' once

  • C

    Infinite loop (i never changes)

    check_circle
  • D

    Prints 'hi' 5 times

Explanation

Forgot to increment i; condition stays true forever.

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

Related questions