Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

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?

⚠ Common exam trap

The PCEP exam often tests the mutable default argument trap — the trap here is that candidates mistakenly believe default arguments are re-evaluated on every function call, leading them to choose option A, when in fact they are evaluated only once at definition time.

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] and [1,1]

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.

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] and [1]

    Why it's wrong here

    The default list is created once at function definition, so both calls mutate the same object: the first returns [1], the second returns [1, 1]. [1] and [1] would require the list to be reset between calls, which never happens here.

  • ✓

    [1] and [1,1]

    Why this is correct

    The default list argument is evaluated once at function definition, so the same mutable list object persists across calls. The first call appends 1, returning [1]; the second appends to that same list, returning [1, 1]. This is Python's classic mutable default argument pitfall.

  • ✗

    TypeError

    Why it's wrong here

    Appending to a list default argument is valid Python and raises no exception; the function returns the mutated list on each call. A TypeError would occur only if the parameter defaulted to an immutable type such as a tuple or string, which cannot be appended to.

  • ✗

    [1] and [2]

    Why it's wrong here

    The mutable default list persists across calls, so the second call appends to the same list already containing 1, returning [1, 1], not [1] and [2]. The [1] and [2] pair would arise only if each call received a fresh list, which the shared default prevents.

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.