Python Functions and Recursion Previous Year Questions (PYQs) for GATE DA: 3+ Solved Questions with Step-by-Step Solutions

    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 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

    Question 2 · Programming, Data Structures and Algorithms 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

    Question 3 · Programming, Data Structures and Algorithms 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)

    More previous year questions (pyqs) in this unit

    chapter
    Python Functions and Recursion Previous Year Questions (PYQs) for GATE DA: 3+ Solved Questions with Step-by-Step Solutions

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

    A question from this chapter

    Question 1
    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?
    Question 2
    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 3
    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)
    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.