ProgrammingModerateAccenture · Cognizant pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
String s = ""
Integer n = 13
while (n > 0)
    s = (n mod 2) + s          // prepend the digit
    n = n / 2
end while
Print s
  1. A. 1011
  2. B. 1101
  3. C. 13
  4. D. 111

Correct answer: B. 1101

13 = 8 + 4 + 1. Remainders come out least significant first (1, 0, 1, 1); prepending puts them in the right order: 1101. Appending instead would give the reversed string 1011.

More Pseudocode and Code Tracing questions

Exams that ask this topic