GATE DA 2026 Q16: Python mutable default argument output
- Programming, Data Structures and Algorithms
- 1 mark
- MCQ
- −1/3 if wrong
Question
Consider the given Python program.
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, []))Which of the following is the correct output of this program? (Each option lists the three printed lines.)
Try it: multiple choice: exactly one correct option
Wrong answer costs 1/3 mark in GATE.
Show answer and explanation
Answer: B (official GATE 2026 final answer key)
Concept used: A default argument is created once, when the function is defined, and shared by every call that uses it.
First step: The first two calls append to the same default list; the third call passes a new empty list.
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
Source: official GATE 2026 DA question paper and final answer key, organised by IIT Guwahati. Question paper · Answer key
More Programming, Data Structures and Algorithms PYQs
- 2026 · Q15Expected-time recurrence for randomised-input quicksort
- 2026 · Q25Binary tree from preorder and inorder traversals
- 2026 · Q31Worst-case comparisons in binary search of 1000 elements
- 2026 · Q39Number of calls made by a Fibonacci-like recursion
- 2026 · Q40Reachability in a graph and its reverse
- 2026 · Q49Comparisons by bubble sort and insertion sort on a nearly sorted array