IAE Academy

GATE 2026 DA Q.16: solution and answer

GATE 2026 · DAQ.16 · 1 mark · MCQ

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?

  1. (A)

    [1]
    [2]
    [3]

  2. (B)

    [1]
    [1, 2]
    [3]

  3. (C)

    [1]
    [2]
    [1, 2, 3]

  4. (D)

    [1]
    [1, 2]
    [1, 3]

See the question in the paper

Answer and solution

ANSWER

The output is [1], [1, 2], [3] on three lines, which is option (B).

Official answer: BSource: Official answer key, GATE 2026 DA (IIT Guwahati)

  1. The default value lst=[] is evaluated once, when the def statement runs. The function object keeps that single list, and every call that omits the second argument uses it. Changes made to it persist between calls.

  2. Call append_to_lst(1): no list is passed, so the shared default list (initially empty) is used. After lst.append(1) it is [1], and the function returns it. The program prints [1].

  3. Call append_to_lst(2): no list is passed, so the same shared default list is used. It already holds 1. After lst.append(2) it is [1, 2]. The program prints [1, 2].

  4. Call append_to_lst(3, []): a new empty list is created at this call site and passed explicitly. The default is not used, and the shared list is untouched. After lst.append(3) this new list is [3]. The program prints [3].

  5. The three lines printed are [1], [1, 2] and [3]. This is option (B).

KEY IDEA

A default argument such as lst=[] is evaluated once, when the function is defined, so every call that omits it shares one mutable list. A list passed explicitly is a different object and does not touch the shared default.

WATCH OUT FOR

Assuming lst=[] creates a fresh empty list on every call, which gives [1], [2], [3] (option A). The second call reuses the list that already holds 1, so it prints [1, 2].

From the GATE 2026 DA paper. Every question of this paper · Download the full paper · All 10 Python questions, with notes

More Python questions