Skip to content
GATEiit

GATE DA 2026 Q50: Python closures sharing an enclosing list

Question

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?

Try it: multiple select: one or more correct options

Select one or more options
Select all correct options. No negative marking, no partial marks.
Show answer and explanation

Answer: B, D (official GATE 2026 final answer key)

Concept used: Each call to outer() creates a new list x, captured by the closure it returns.

First step: f1 and f2 come from separate calls, so each has its own x: f1's list grows 10, 20, 40 and f2's holds only 30.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2026 DA question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key