Question Details

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

Options

A

[1] [1,2] [3]

B

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

C

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

D

[1] [2] [3]

Show Answer
Correct Answer
A [1] [1,2] [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]

Transparent Access on Mindyard

Built for Learners & Educators Alike

Completely free for students. Educators & coaching institutes get 2 months free Pro trial to create tests.

FREE FOR STUDENTS

Practice & Learn Free for Students

Access unlimited practice questions, detailed step-by-step solutions, and exam study materials — completely free for students.

  • Unlimited mock tests & question bank practice
  • Full access to curated study library & formula notes
  • 100% free for students • No credit card required
2 MONTHS FREE TRIAL

Educators & Coaching Institutes

Create and conduct professional online assessments with AI question generation, tab proctoring, and comprehensive batch management.

  • AI question uploader & rapid test generator
  • Anti-cheating tab proctoring & performance reports
  • Full Pro access free for 60 days • No upfront commitment
Discover more resources

You may also like

Mock Tests

View All
  • GATE
  • beginner
  • 3 hours
  • civil engineering

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.

  • GATE
  • beginner
  • 3 hours
  • electronics and communication engineering

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.

Ask AI Tutor
5 left
Q1 View Question & Options
AI Tutor is solving this question...
Reading question context & options...
Ask your own doubt or choose a quick prompt: