ProgrammingHardCapgemini · Infosys

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

What will be the output of the following pseudocode? (All variables are integers, so / is integer division)
Function f(Integer n)
    if (n < 10)
        return n
    end if
    return f(n / 10) - n mod 10
End Function
Print f(5274)
  1. A. 2
  2. B. -8
  3. C. -4
  4. D. 12

Correct answer: B. -8

Unwinding: f(5) = 5, f(52) = 5 − 2 = 3, f(527) = 3 − 7 = −4, f(5274) = −4 − 4 = −8.

More Pseudocode and Code Tracing questions

Exams that ask this topic