while Loops

AP Computer Science A· difficulty 3/5

int n = 16;
int count = 0;
while (n > 1) {
  n /= 2;
  count++;
}
System.out.println(count);

What is printed?

  • A

    4

    check_circle
  • B

    3

  • C

    5

  • D

    16

Explanation

n: 16 -> 8 -> 4 -> 2 -> 1, with count 1, 2, 3, 4. After count=4, n=1, condition n>1 false. count = 4.

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

Related questions