ProgrammingHardCapgemini · Infosys

What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings)

What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings)
Function bin(Integer n)
    if (n == 0)
        return ""
    end if
    return bin(n / 2) + toString(n mod 2)
End Function
String b = bin(37)
Print b, length(b), length(bin(0))
  1. A. 100101 6 1
  2. B. 101001 6 0
  3. C. 100101 6 0
  4. D. 100101 5 0

Correct answer: C. 100101 6 0

The recursion writes the binary digits most significant first: 37 = 100101 (6 digits). bin(0) returns the empty string, so its length is 0 — a known gap of this version. Output 100101 6 0.

More Pseudocode and Code Tracing questions

Exams that ask this topic