PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A function `def process(data):` modifies the dictionary passed as argument by adding a new key. The developer wants to avoid modifying the original dictionary. What should the function do?
⚠ Common exam trap
Python Institute often tests the misconception that mutable objects are passed by value or that changes inside a function are local, leading candidates to incorrectly choose Option C, which is false for mutable types like dictionaries and lists.
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
✓
Create a copy of the dictionary at the start: `data = data.copy()`
Dictionaries are mutable objects in Python, so passing a dictionary to a function passes a reference to the same object. Calling `data.copy()` creates a shallow copy of the dictionary, allowing the function to modify the copy without affecting the original dictionary. This is the standard Pythonic way to avoid side effects on mutable 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.
- ✓
Create a copy of the dictionary at the start: `data = data.copy()`
Why this is correct
Dictionaries are mutable and passed by object reference, so adding a key inside the function mutates the caller's original. Calling data.copy() creates a shallow copy bound to the local name, so subsequent key additions affect only the copy, leaving the caller's dictionary unchanged.
- ✗
Add the key, then delete it at the end.
Why it's wrong here
Adding then deleting the key still mutates the caller's dictionary during execution, and any failure before deletion leaves the original altered. It is tempting as an attempt to restore state, but it does not prevent mutation; copying the dictionary inside the function is what preserves the original.
- ✗
Modify directly; changes to mutable objects are local only.
Why it's wrong here
Python passes dictionaries by object reference, so mutating one inside the function alters the caller's original object — the opposite of the stated goal. Copying the dictionary first (for example with dict(data) or data.copy()) is what preserves the original. The option confuses local rebinding of the name with in-place mutation of the shared object.
- ✗
Convert the dictionary to a tuple before processing.
Why it's wrong here
Converting a dictionary to a tuple discards the key-value mapping entirely, so the function can no longer add a key or perform dictionary operations. It is tempting as a way to make data immutable, but tuples only prevent reassignment of elements, not the mutation the developer wants to avoid.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 482-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 →
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.