ProgrammingModerateInfosys SP · TCS NQT

What will be the output of the following pseudocode? (& is AND, >> is right shift)

What will be the output of the following pseudocode? (& is AND, >> is right shift)
Integer x = 40, pos = 1
while ((x & 1) == 0)
    x = x >> 1
    pos = pos + 1
end while
Integer n = 1000, bits = 0
while (n > 0)
    bits = bits + 1
    n = n >> 1
end while
Print pos, bits
  1. A. 3 9
  2. B. 4 9
  3. C. 3 10
  4. D. 4 10

Correct answer: D. 4 10

40 = 101000, so its lowest 1-bit is the 4th from the right: pos = 4. 1000 lies between 2⁹ = 512 and 2¹⁰ = 1024, so it needs 10 bits. Output 4 10.

More Pseudocode and Code Tracing questions

Exams that ask this topic