ProgrammingVery hardGoldman Sachs OA · Infosys SP

What will be the output of the following pseudocode?

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

Correct answer: D. 32

Let C(n) be the calls made by p(n). C(0) = 1 and C(n) = 1 + C(n − 1) + … + C(0), which doubles each time: 1, 2, 4, 8, 16, 32. So stars = 32.

More Pseudocode and Code Tracing questions

Exams that ask this topic