Courseiva
Functions, Tuples, Dictionaries and ExceptionshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A function receives a dictionary that may contain nested dictionaries. The function must modify the dictionary without affecting the original passed argument. Which technique ensures a complete independent copy?

⚠ Common exam trap

The PCEP exam often tests the distinction between shallow and deep copies, and the trap here is that candidates assume `dict.copy()` or `copy.copy()` create a full independent copy, overlooking that nested dictionaries remain shared references.

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

Use copy.deepcopy() from the copy module

`copy.deepcopy()` recursively copies all objects within the dictionary, including nested dictionaries, creating a completely independent copy. This ensures modifications to the copy do not affect the original argument, which is required when the dictionary contains mutable nested structures.

Answer analysis

Option-by-option breakdown

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

  • Use copy.deepcopy() from the copy module

    Why this is correct

    Deep copy recursively copies all objects, making the new dictionary completely independent.

  • Assign the dictionary to a new variable (e.g., new_dict = original)

    Why it's wrong here

    Assignment does not copy the dictionary; it just creates a new reference to the same object.

  • Use copy.copy() on the original dictionary

    Why it's wrong here

    Shallow copy does not duplicate nested mutable objects.

  • Use dict.copy() method

    Why it's wrong here

    Same as copy.copy(), creates a shallow copy.

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 →

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.