The GATE Grind

GATE 2026 DA – Question 50

Programming, Data Structures and Algorithms · Programming in Python · 2 marks · Multiple select

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. f1 and f2 share the same list x
  2. Output of Line Q is [10, 20]
  3. Output of Line R is [10, 20, 30]
  4. Output of Line S is [10, 20, 40]

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).