ProgrammingHardInfosys · Capgemini pattern

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer Function f(Integer n)
    if (n == 0) return 0
    return f(n / 2) * 10 + (n mod 2)
End Function
Print f(19)
  1. A. 10011
  2. B. 11001
  3. C. 19
  4. D. 1011

Correct answer: A. 10011

The function writes n's binary digits as a decimal number: the recursion handles the higher bits first, then appends n mod 2. 19 = 16 + 2 + 1 = 10011₂. Building the digits in the loop order without recursion would reverse them (11001).

More Pseudocode and Code Tracing questions

Exams that ask this topic