while Loops

AP Computer Science A· difficulty 3/5

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

What is printed?

  • A

    6

    check_circle
  • B

    5

  • C

    7

  • D

    64

Explanation

n: 64 -> 32 (count=1) -> 16 (2) -> 8 (3) -> 4 (4) -> 2 (5) -> 1 (6). When n=1, condition n>1 is false, exit. count = 6 (which is log2(64)).

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

Related questions