ProgrammingVery hardProduct company OA

What will be the output of the following pseudocode? (calls counts every call of f, including the first.)

What will be the output of the following pseudocode? (calls counts every call of f, including the first.)
Integer calls = 0
Function f(Integer n)
    calls = calls + 1
    if (n > 100)
        return n - 10
    end if
    return f(f(n + 11))
End Function
Print f(99), calls
  1. A. 91 3
  2. B. 100 5
  3. C. 91 5
  4. D. 89 3

Correct answer: C. 91 5

f(99) → f(f(110)); f(110) = 100. Then f(100) → f(f(111)); f(111) = 101. Then f(101) = 91. Calls: f(99), f(110), f(100), f(111), f(101) = 5, result 91 (this is McCarthy's 91 function: every n ≤ 100 gives 91). '89' applies n − 10 to 99 directly.

More Pseudocode and Code Tracing questions

Exams that ask this topic