GATE 2026 DA – Question 50
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 SWhich of the following options is/are correct?
Practise this question in The GATE Grind →
Show answer and explanation
Correct answer: (B) Output of Line Q is [10, 20]; (D) Output of Line S is [10, 20, 40]
Explanation
Each call of outer() creates a new list x, and inner keeps a reference to it. So f1 and f2 have separate lists (A is false). f1(10) prints [10], f1(20) prints [10, 20] (B is true), f2(30) prints [30] because it has its own list (C is false), and f1(40) prints [10, 20, 40] (D is true).