GATE DA 2026, Q.16: Python default list shared across calls
The question
What does this program print?
def append_to_lst(val, lst=[]):
lst.append(val)
return lst
print(append_to_lst(1))
print(append_to_lst(2))
print(append_to_lst(3, []))- (A) [1] / [2] / [3]
- (B) [1] / [1, 2] / [3]
- (C) [1] / [2] / [1, 2, 3]
- (D) [1] / [1, 2] / [1, 3]
Watch it solved
Solve it step by step
- Defaults are evaluated once. lst = [] is created when the function is defined, not on each call.
- Call 1. Appends 1 to the shared default: [1].
- Call 2. Same list again: [1, 2].
- Call 3. A fresh list is passed in: [3].
Answer: (B) [1] / [1, 2] / [3]
Official IIT Guwahati answer key: B.
Common mistake. Expecting a new empty list on every call (option A).
Remember. Never use a mutable default; write lst=None and create the list inside.
Want every question solved? The past-paper pack has all 195 questions from 2024, 2025 and 2026 for ₹499. The cheat sheet pack has 23 printable sheets for ₹399.
See the past-paper pack See the cheat sheetsSource: official GATE DA 2026 question paper and answer key (IIT Guwahati). The question is lightly reworded; every number and option is kept.