ProgrammingVery hardTCS NQT · Infosys · Accenture

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer n = 37
String out = ""
while (n > 0)
    out = out + (n mod 2)
    n = n / 2
end while
Print out
  1. A. 100101
  2. B. 010011
  3. C. 101010
  4. D. 101001

Correct answer: D. 101001

Remainders arrive from the least significant bit: 1, 0, 1, 0, 0, 1. Since they are appended (not prepended), the string is 101001, the reverse of (37)2 = 100101.

More Pseudocode and Code Tracing questions

Exams that ask this topic