ProgrammingHardTCS NQT · Accenture pattern

What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.)

What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, ^ is bitwise XOR, << and >> are left and right shifts.)
Integer n = 1, c = 0
while (n < 200)
    n = n << 1
    c = c + 1
end while
Print c, n
  1. A. 8 128
  2. B. 7 128
  3. C. 8 256
  4. D. 9 512

Correct answer: C. 8 256

Each shift doubles n: 2, 4, 8, ..., 128, 256. The test 128 < 200 still passes, so the eighth shift makes n = 256 and then the loop stops.

More Pseudocode and Code Tracing questions

Exams that ask this topic