ProgrammingModerateInfosys · Wipro pattern

What will be the output of the following pseudocode? (/ is integer division.)

What will be the output of the following pseudocode? (/ is integer division.)
Function f(Integer n)
    if (n == 0)
        return 0
    end if
    return (n mod 2) + 10 * f(n / 2)
End Function
Print f(13)
  1. A. 1011
  2. B. 13
  3. C. 1101
  4. D. 111

Correct answer: C. 1101

The last binary digit (n mod 2) becomes the units digit and the rest is shifted one decimal place left: f(13) = 1 + 10·f(6), f(6) = 0 + 10·f(3), f(3) = 1 + 10·f(1), f(1) = 1. So f(13) = 1101, the binary form of 13 written as a decimal number. 1011 is the reversed digit order.

More Pseudocode and Code Tracing questions

Exams that ask this topic