GATE DA 2026 · Programming in Python

MCQ · 2 marks · Problem PROG-PYTH-0013

A recursive function in Python is given.
def mystery(n):
    if n <= 0:
        return 1
    return mystery(n - 1) + mystery(n - 2)
Now, consider the following function call: mystery(4)Assume that a typical runtime stack is used to manage function calls. Each function call is pushed onto the stack and removed only after it finishes execution.Which of the following options denotes the total number of function calls (i.e., the total number of stack activations), including the initial call, to compute mystery(4)?
  1. A
    5
  2. B
    9
  3. C
    15
  4. D
    17

Solve it on GATE DA practice →More Python Programming problems

Show the worked solutionLet \(C(n)\) be the number of calls (including the first) made to evaluate mystery(n). A call with \(n \le 0\) makes no further calls; any other call makes two, on \(n-1\) and \(n-2\):\[\boxed{C(n)=1+C(n-1)+C(n-2)\ \text{ for } n>0,\qquad C(n)=1\ \text{ for } n\le 0}\]\(C(0)=1\), \(C(-1)=1\)\(C(1)=1+C(0)+C(-1)=3\)\(C(2)=1+C(1)+C(0)=5\)\(C(3)=1+C(2)+C(1)=9\)\(C(4)=1+C(3)+C(2)=15\)Answer: C

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com