PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
Exhibit
def add_item(item, lst=[]):
lst.append(item)
return lst
print(add_item(1))
print(add_item(2, []))
print(add_item(3))Refer to the exhibit. What is the output?
⚠ Common exam trap
The PCEP exam often tests the mutable default argument trap, where candidates mistakenly believe each function call creates a fresh default list, rather than understanding that the default object is created once and reused, leading to cumulative modifications across calls.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
[1] [2] [1, 3]
The function uses a mutable default argument `lst=[]`. On the first call, `append_to_list(1)` uses the default list and appends 1, returning `[1]`. On the second call, `append_to_list(2, [])` passes an explicit empty list, so it appends 2 to that new list, returning `[2]`. On the third call, `append_to_list(3)` uses the default list again, which now contains `[1]` from the first call, so appending 3 yields `[1, 3]`. Thus the output is `[1] [2] [1,3]` printed on separate lines.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
[1] [2] [3]
Why it's wrong here
Incorrect; third call should contain 1 and 3.
- ✗
Error: default argument is mutable
Why it's wrong here
No error; code runs, though it's a common pitfall.
- ✗
[1] [1, 2] [1, 2, 3]
Why it's wrong here
Incorrect; second call uses a new list, so only [2].
- ✓
[1] [2] [1, 3]
Why this is correct
Correct; default list persists across calls.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on PCEP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. A developer writes a function that appends an item to a list: def add_item(item, my_list=[]): my_list.append(item); return my_list. They call add_item(1) twice. What are the return values of the two calls?
medium- A.[1] and [1]
- ✓ B.[1] and [1,1]
- C.TypeError
- D.[1] and [2]
Why B: The default argument `my_list=[]` is evaluated only once at function definition time, not each time the function is called. Therefore, the first call `add_item(1)` appends 1 to the same list object, returning `[1]`. The second call appends another 1 to that same list, returning `[1, 1]`. This is a classic Python gotcha involving mutable default arguments.
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCEP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCEP exam.