Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer defines a function `def add_item(item, basket=[]):` and calls it multiple times without providing a basket argument. After three calls with different items, what is the content of the default basket?

⚠ Common exam trap

The trap here is assuming that default arguments are re-evaluated on each call, leading to the belief that the basket would be empty or contain only the latest item.

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

✓

The basket contains all three items from the three calls, because the default list is shared and mutated.

Default arguments are evaluated once at function definition, so a mutable default like a list is shared across all calls that do not provide an explicit argument. The list is modified in place with each append, causing all items to accumulate. The other options incorrectly assume per-call initialization, overwriting, or a runtime error.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    Each call gets a fresh empty list, so the basket remains empty after each call.

    Why it's wrong here

    Default arguments are evaluated only once at function definition time, not on each call. Therefore, the same list object is reused across calls. This option incorrectly assumes that a new list is created per call, which is not how Python handles default mutable arguments.

  • ✗

    A TypeError is raised because mutable default arguments are not allowed in Python.

    Why it's wrong here

    Python allows mutable default arguments; they are syntactically valid and do not raise a TypeError. However, they can lead to unexpected behavior as described. This option is incorrect because no error occurs; the function works but with unintended side effects.

  • ✗

    The basket contains only the item from the most recent call, as previous items are overwritten.

    Why it's wrong here

    The default list is not overwritten; it accumulates items because it is the same object. Each call appends to the existing list, so previous items are retained. This option misunderstands the behavior of list.append, which modifies the list in place rather than replacing it.

  • ✓

    The basket contains all three items from the three calls, because the default list is shared and mutated.

    Why this is correct

    This is correct because the default argument `basket=[]` is evaluated once when the function is defined, creating a single list object. Each call that omits the basket argument uses that same list and appends the item, so all three items accumulate. This is a classic Python pitfall with mutable default arguments.

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 and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

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.