Function Scope, Closures and Default Arguments Previous Year Questions (PYQs) for GATE DA: 2+ Solved Questions with Step-by-Step Solutions

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

    More previous year questions (pyqs) in this unit

    chapter
    Function Scope, Closures and Default Arguments Previous Year Questions (PYQs) for GATE DA: 2+ Solved Questions with Step-by-Step Solutions

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

    A question from this chapter

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