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, []))
Solution :
The correct option is [1] [1,2] [3].
This question tests our understanding of default argument evaluation in Python, specifically when using a mutable object like a list as a default parameter value.
Step-by-step Explanation:
1. Default Argument Evaluation:
In Python, default arguments are evaluated only once when the function definition is executed, not each time the function is called. This means that if we use a mutable object like a list (lst=[]) as a default argument, the same list object is shared across all function calls that do not provide an explicit argument for that parameter.
2. First Call: print(append_to_lst(1))
Since no list is explicitly passed for the second parameter, Python uses the default list created during function definition.
The number 1 is appended to this default list, making it [1].
The function returns this list, and the code prints:
[1]
3. Second Call: print(append_to_lst(2))
Again, no list is explicitly passed. Python uses the exact same default list object, which currently contains [1].
The number 2 is appended to this shared list, modifying its content to [1, 2].
The function returns this list, and the code prints:
[1, 2]
4. Third Call: print(append_to_lst(3, []))
In this call, a new, empty list [] is explicitly provided as the second argument (lst).
Since an explicit argument is provided, Python does not use the default list.
The number 3 is appended to this newly created list, resulting in [3].
The function returns this new list, and the code prints:
[3]
Combining the outputs of all three calls, we get:
[1]
[1, 2]
[3]
Completely free for students. Educators & coaching institutes get 2 months free Pro trial to create tests.
Access unlimited practice questions, detailed step-by-step solutions, and exam study materials — completely free for students.
Create and conduct professional online assessments with AI question generation, tab proctoring, and comprehensive batch management.
Prepare for GATE 2026 Civil Engineering with the latest question papers, previous year papers, detailed solutions, syllabus, exam pattern, preparation tips, mock tests, and subject-wise study material. Download GATE 2026 CE question papers with solutions in PDF format and boost your preparation for top engineering institutes and PSU recruitment.
Prepare effectively for GATE 2026 Electronics & Communication Engineering (EC) with this comprehensive Question Paper with Solutions. This resource includes detailed explanations, concept-wise solutions, previous year paper analysis, and exam-oriented practice to help aspirants strengthen core subjects like Electronic Devices, Analog Circuits, Digital Circuits, Signals & Systems, Network Theory, Control Systems, Electromagnetics, Communications, and more. Perfect for revision, self-assessment, and boosting your GATE 2026 EC preparation strategy.
Copyright © 2026 Combe Labs Private Limited. All Rights Reserved.