chapter
    Python Functions and Recursion PYQs for GATE DA

    Solve 3+ Python Functions and Recursion previous year questions for GATE DA with answers and detailed solutions. Free sample questions below.

    Try a question

    Answer it here to see how it works. Nothing is recorded until you sign in.

    Question 1
    2026 PYQ
    Level 3: Exam Standard
    A recursive function in Python is given.

    def mystery(n):
        if n <= 0:
            return 1
        else:
            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)?
    Question 2
    2025 PYQ
    Level 3: Exam Standard
    Consider the following Python code snippet.

    def f(a,b):
    if (a==0):
    return b
    if (a%2==1):
    return 2*f((a-1)/2,b)
    return b+f(a-1,b)
    print(f(15,10))

    The value printed by the code snippet is (Answer in integer)
    Question 3
    2024 PYQ
    Level 3: Exam Standard
    Consider the following Python code:
    def count(child_dict, i):
    if i not in child_dict.keys():
    return 1
    ans = 1
    for j in child_dict[i]:
    ans += count(child_dict, j)
    return ans
    child_dict = dict()
    child_dict[0] = [1,2]
    child_dict[1] = [3,4,5]
    child_dict[2] = [6,7,8]
    print(count(child_dict,0))
    Which ONE of the following is the output of this code?
    Free preview ends here

    Login to view the complete previous-year questions and solutions

    Creating an account is free. You get the rest of this chapter, step-by-step solutions, and a study plan built around the topics you are actually weak at.

    Why MastersUp

    Personalised first. High quality throughout.

    Most platforms hand everyone the same content. Here the content moves with your performance, topic by topic.

    Built around you, not around a syllabus PDF

    Every answer you give moves your topic-level intelligence rate. The next question, the next revision card and tomorrow's plan all change with it.

    Revision that hits your weak spots

    We only revise topics you have actually attempted and are still below the safe bar on — never the same chapter on repeat.

    Questions calibrated to the real exam

    Each question carries a measured toughness. You are served a rung above your current level, so practice keeps stretching you.

    Notes written for recall, not for volume

    Full lesson cards for first study, curated short-note cards for the last mile — with derivations, traps and exam patterns marked.

    One place for everything

    Notes, chapter practice, previous-year questions, test series and full-length papers — all feeding one picture of your preparation.

    Honest progress

    No vanity streaks. Progress here means chapters mastered and accuracy that held up on harder questions.

    Unlock the whole course

    Full notes and short notes, the complete question bank with worked solutions, mock tests, full-length papers, and an adaptive plan that rebuilds itself as you improve.

    Python Functions and Recursion PYQs for GATE DA

    Solve 3+ Python Functions and Recursion previous year questions for GATE DA with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: Python Functions and Recursion

    Chapter Roadmap: Python Functions and Recursion

    1
    Recursive Function Tracing and Return Values
    Core Focus. Execution flow, stack frames, return propagation.
    2
    Chapter Mastery Achieved

    The Mental Model: Stack Frames as Isolated Worlds

    The Mental Model: Stack Frames as Isolated Worlds

    Recursion is not a loop that reuses memory. It is a chain of independent function invocations.

    Frame 3: ans = 1
    Frame 2: ans = 1
    Frame 1: ans = 1
    • Each call New Stack Frame (new local namespace)
    • Parent state is frozen at the call site
    • Execution resumes only after child returns a value
    • Variables like n, ans, or i are distinct copies per frame
    Key Insight: When tracing, never update a variable "globally" across recursive calls unless explicitly passed by reference (which Python primitives do not support). Each ans = 1 in a new call starts fresh.

    Python Functions and Recursion: Solved Questions with Step-by-Step Explanations (3 Problems)

    Question 1 · Programming, Data Structures and Algorithms · 2026 MCQ
    A recursive function in Python is given.

    def mystery(n):
        if n <= 0:
            return 1
        else:
            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

    Correct Answer:

    C

    Step-by-Step Solution

    Insight: The total number of function calls follows a recurrence relation , where the accounts for the current function call itself.

    Exam route: Compute iteratively from base cases. . Then , , , .

    Learning route: Draw the recursion tree for mystery(4). The root is 1 call. It branches to mystery(3) and mystery(2). Count all nodes in this call tree. Total nodes = 15. Note that mystery(0) and mystery(-1) are base cases that make 1 call each and return immediately without further branching.

    Question 2 · Programming, Data Structures and Algorithms · 2025 NAT
    Consider the following Python code snippet.

    def f(a,b):
    if (a==0):
    return b
    if (a%2==1):
    return 2*f((a-1)/2,b)
    return b+f(a-1,b)
    print(f(15,10))

    The value printed by the code snippet is (Answer in integer)
    Correct Answer:

    160.00

    Step-by-Step Solution

    Insight: The function evaluates conditions sequentially. Since is always odd in the recursive steps, it always takes the a%2==1 branch, multiplying the result by 2 at each step until .

    Exam route: .

    Learning route: Trace frame by frame. Note that (a-1)/2 in Python 3 produces a float (e.g., 7.0). However, a%2==1 still evaluates to True for 7.0 % 2 == 1.0. The base case a==0 catches 0.0. The return value is multiplied by 2 exactly 4 times.

    Question 3 · Programming, Data Structures and Algorithms · 2024 MCQ
    Consider the following Python code:
    def count(child_dict, i):
    if i not in child_dict.keys():
    return 1
    ans = 1
    for j in child_dict[i]:
    ans += count(child_dict, j)
    return ans
    child_dict = dict()
    child_dict[0] = [1,2]
    child_dict[1] = [3,4,5]
    child_dict[2] = [6,7,8]
    print(count(child_dict,0))
    Which ONE of the following is the output of this code?
    1. A.

      6

    2. B.

      1

    3. C.

      8

    4. D.

      9

    Correct Answer:

    D

    Step-by-Step Solution

    Insight: The function computes the total number of nodes in a tree represented by an adjacency list, where each node counts itself (ans = 1) and adds the counts of its children via return values.

    Exam route: Draw the tree. Root 0 has children 1, 2. Node 1 has 3, 4, 5. Node 2 has 6, 7, 8. Total nodes = 1 (root) + 1 (node 1) + 3 (leaves of 1) + 1 (node 2) + 3 (leaves of 2) = 9.

    Learning route: Trace using stack frames. Frame 0 calls Frame 1 and Frame 2. Frame 1 calls 3, 4, 5 which return 1 each. Frame 1 returns . Frame 2 similarly returns 4. Frame 0 returns . The key is that ans = 1 resets in every frame, so it is return-value aggregation, not a shared global counter.

    More previous year questions (pyqs) in this unit