chapter
    Function Scope, Closures and Default Arguments PYQs for GATE DA

    Solve 2+ Function Scope, Closures and Default Arguments 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
    Consider the given Python program.

    def outer():
        x = []
        def inner(val):
            x.append(val)
            return x
        return inner

    f1 = outer()
    f2 = outer()
    print(f1(10)) # Line P
    print(f1(20)) # Line Q
    print(f2(30)) # Line R
    print(f1(40)) # Line S

    Which of the following options is/are correct?
    Question 2
    2026 PYQ
    Level 3: Exam Standard
    Consider the given Python program.

    def append_to_lst(val, lst=[]):
        lst.append(val)
        return lst
    print(append_to_lst(1))
    print(append_to_lst(2))
    print(append_to_lst(3, []))

    Which of the following is the correct output of this program?
    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.

    Function Scope, Closures and Default Arguments PYQs for GATE DA

    Solve 2+ Function Scope, Closures and Default Arguments previous year questions for GATE DA with answers and detailed solutions. Free sample questions below.

    Chapter Roadmap: Function Scope, Closures and Default Arguments

    Chapter Roadmap

    Chapter Journey

    1
    Mutable Default Arguments (Current Topic)
    • Why default arguments are evaluated only once
    • The shared state trap with lists and dictionaries
    • The None pattern for safe defaults
    2
    Closures and Enclosed State
    • How inner functions capture variables from outer scopes
    • The nonlocal keyword and state retention
    • Factory functions and decorators

    Chapter Mastery

    • Predict the exact state of mutable default arguments across multiple function calls.
    • Distinguish between variable mutation and reassignment in default arguments.
    • Trace closure state and identify enclosed variables in nested functions.
    Weight hint: Both topics are highly conceptual. Expect code-tracing questions where a small misunderstanding of evaluation time or scope leads to the wrong output.

    Topic Hero: The Mutable Default Argument Trap

    Topic Hero: The Mutable Default Argument Trap

    The Core Intuition

    In Python, default arguments are evaluated exactly once — when the def statement is executed, not when the function is called.

    If the default value is a mutable object (like a list, dictionary, or set), that single object is shared across all function calls that use the default.

    Why this happens

    When Python reads a function definition, it evaluates the default expressions and stores them in the function object. Every time you call the function without providing that argument, Python passes the exact same object reference.

    Argument Type Evaluated When Behavior
    Immutable (int, str, tuple) Definition time Safe. Reassignment creates a new local object.
    Mutable (list, dict, set) Definition time Trap. Mutations affect the shared default object.

    Function Scope, Closures and Default Arguments: Solved Questions with Step-by-Step Explanations (2 Problems)

    Question 1 · Programming, Data Structures and Algorithms · 2026 MSQ
    Consider the given Python program.

    def outer():
        x = []
        def inner(val):
            x.append(val)
            return x
        return inner

    f1 = outer()
    f2 = outer()
    print(f1(10)) # Line P
    print(f1(20)) # Line Q
    print(f2(30)) # Line R
    print(f1(40)) # Line S

    Which of the following options is/are correct?
    1. A.

      f1 and f2 share the same list x

    2. B.

      Output of Line Q is [10, 20]

    3. C.

      Output of Line R is [10, 20, 30]

    4. D.

      Output of Line S is [10, 20, 40]

    Correct Answer:

    ["B","D"]

    Step-by-Step Solution

    Insight: Each call to the outer function creates a completely independent closure with its own isolated cell objects for enclosed variables.

    Exam route: f1 and f2 are created by separate calls to outer(), so they have separate x lists. f1(10) -> [10], f1(20) -> [10, 20] (Line Q). f2(30) -> [30] (Line R). f1(40) -> [10, 20, 40] (Line S).

    Learning route: This is a closure instance isolation question, recognizable by the nested function returning an inner function that modifies an enclosed mutable variable.

    Step 1: When f1 = outer() is executed, a new list x is created in outer's local scope. The inner function captures this specific list in its closure cell. f1 now points to this specific inner function instance.

    Step 2: When f2 = outer() is executed, a completely new list x is created. A new inner function instance is created, capturing this new list. f2 points to this second instance. f1 and f2 do not share any state.

    Step 3: Line P: f1(10) appends 10 to f1's list. Returns [10].

    Step 4: Line Q: f1(20) appends 20 to f1's list. Returns [10, 20]. Option B is correct.

    Step 5: Line R: f2(30) appends 30 to f2's list. Since f2's list is independent and starts empty, it returns [30]. Option C is incorrect.

    Step 6: Line S: f1(40) appends 40 to f1's list. Returns [10, 20, 40]. Option D is correct.

    Answer: Options B and D.

    Question 2 · Programming, Data Structures and Algorithms · 2026 MCQ
    Consider the given Python program.

    def append_to_lst(val, lst=[]):
        lst.append(val)
        return lst
    print(append_to_lst(1))
    print(append_to_lst(2))
    print(append_to_lst(3, []))

    Which of the following is the correct output of this program?
    1. A. [1]
      [2]
      [3]
    2. B. [1]
      [1, 2]
      [3]
    3. C. [1]
      [2]
      [1, 2, 3]
    4. D. [1]
      [1, 2]
      [1, 3]
    Correct Answer:

    B

    Step-by-Step Solution

    Insight: Default arguments are evaluated exactly once at definition time; mutating a mutable default object persists its state across all subsequent calls that use the default.

    Exam route: Call 1 uses the default empty list, appends 1, and returns [1]. The default list is now [1]. Call 2 uses that same default list, appends 2, and returns [1, 2]. Call 3 explicitly passes a new empty list, bypassing the default, appends 3, and returns [3].

    Learning route: This is a classic mutable default argument question, recognizable by the lst=[] parameter. When Python executes the def statement, it creates a single list object and stores it as the default.

    Step 1: In the first call append_to_lst(1), no list is provided, so lst binds to the default []. The append method mutates this default object to [1]. The function returns [1].

    Step 2: In the second call append_to_lst(2), no list is provided, so lst binds to the exact same default object, which is now [1]. The append method mutates it to [1, 2]. The function returns [1, 2].

    Step 3: In the third call append_to_lst(3, []), a new list is explicitly provided. The parameter lst binds to this new object, completely ignoring the default. The append method mutates the new list to [3]. The function returns [3].

    The final output is [1], then [1, 2], then [3].

    Answer: Option B.

    More previous year questions (pyqs) in this unit