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
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).
More Programming in Python questions
GATE 2026 DA Q58– Consider the given Python program. The output of the program is . (*Answer in integer*)
GATE 2025 DA Q23– Consider the following Python declarations of two lists. Which one of the following…
GATE 2025 DA Q47– Consider the following Python code snippet. When the above program is executed, at the…
GATE 2025 DA Q63– Consider the following Python code snippet. The value printed by the code snippet is…
GATE 2024 DA Q38– Consider the following Python code: Which ONE of the following is the output of this code?
GATE 2024 DA Q39– Consider the function computeS(X) whose pseudocode is given below: Which ONE of the…