ProgrammingHardGoldman Sachs OA · TCS Digital

What will be the output of the following pseudocode?

What will be the output of the following pseudocode?
Integer calls = 0
Procedure f(Integer n)
    calls = calls + 1
    for (each i from 1 to n - 1)
        f(i)
    end for
End Procedure
f(5)
Print calls
  1. A. 15
  2. B. 16
  3. C. 10
  4. D. 32

Correct answer: B. 16

Let C(n) be the calls made by f(n): C(1) = 1 and C(n) = 1 + C(1) + … + C(n − 1), which doubles: 1, 2, 4, 8, 16. calls = 16.

More Pseudocode and Code Tracing questions

Exams that ask this topic