IAE Academy

GATE 2026 DA Q.50: solution and answer

GATE 2026 · DAQ.50 · 2 marks · 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]

See the question in the paper

Answer and solution

ANSWER

The correct options are (B) and (D).

Official answer: B, DSource: Official answer key, GATE 2026 DA (IIT Guwahati)

  1. Each call to outer() runs its body afresh, so it creates a new list x and a new function object inner that captures that list.

  2. f1 = outer() makes f1 a closure over a list, call it . f2 = outer() makes f2 a closure over a different list . Both lists start empty.

  3. Line P: f1(10) appends 10 to and returns itself, so it prints [10].

  4. Line Q: f1(20) appends 20 to , which now holds 10 and 20, so it prints [10, 20]. Option (B) is true.

  5. Line R: f2(30) appends 30 to , which was still empty, so it prints [30], not [10, 20, 30]. Option (C) is false.

  6. Line S: f1(40) appends 40 to , which held [10, 20], so it prints [10, 20, 40]. Option (D) is true.

  7. Option (A) is false: f1 and f2 capture separate lists, as the output of Line R shows. The correct options are B and D.

KEY IDEA

A closure captures the variable of the specific call to outer() that created it. Every call to outer() makes a fresh list, so each returned function keeps its own list. append changes that list in place, so the earlier values stay in it.

WATCH OUT FOR

Assuming f1 and f2 share one list because they come from the same outer definition, which leads to choosing (A) and (C). The list is created inside outer, so it is new on every call and is not shared.

From the GATE 2026 DA paper. Every question of this paper · Download the full paper · All 10 Python questions, with notes

More Python questions