while Loops

AP Computer Science A· difficulty 3/5

int n = 5;
while (n > 0) {
  System.out.println(n);
  n++;
}

What happens?

  • A

    prints 5 once

  • B

    prints nothing

  • C

    prints 5 4 3 2 1

  • D

    infinite loop (until int overflow)

    check_circle

Explanation

n starts at 5 and only increases (n++). The condition n > 0 stays true forever (until int overflow wraps to a negative value far in the future). This is an infinite loop bug — likely the author meant n--.

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

Related questions