Gradient LabSign up free

Home / GATE DA / 2026 / Q.16

GATE DA 2026, Q.16: Python default list shared across calls

ProgrammingMutable default arguments1 markMCQFree

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, []))

Watch it solved

Solve it step by step

  1. Defaults are evaluated once. lst = [] is created when the function is defined, not on each call.
  2. Call 1. Appends 1 to the shared default: [1].
  3. Call 2. Same list again: [1, 2].
  4. 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 sheets

Source: official GATE DA 2026 question paper and answer key (IIT Guwahati). The question is lightly reworded; every number and option is kept.