Courseiva
Functions, Tuples, Dictionaries and ExceptionshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

Exhibit

Refer to the exhibit.

data = {'a': 1, 'b': 2, 'c': 3}
for key, value in data.items():
    if value % 2 == 0:
        data.pop(key)
print(data)

What is the output of this code?

⚠ Common exam trap

Python Institute often tests the misconception that deleting keys during iteration will silently skip or partially modify the dictionary, but Python explicitly forbids size changes during iteration to enforce safe iteration contracts.

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

RuntimeError: dictionary changed size during iteration

Modifying a dictionary's size (adding or deleting keys) during iteration over its keys, values, or items raises a RuntimeError. In this code, the loop iterates over the dictionary's keys while deleting them, which changes the dictionary's size and triggers the exception.

Answer analysis

Option-by-option breakdown

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

  • RuntimeError: dictionary changed size during iteration

    Why this is correct

    Modifying dict while iterating over its items raises RuntimeError.

  • {}

    Why it's wrong here

    Not the output.

  • {'b': 2}

    Why it's wrong here

    Not the output.

  • {'a': 1, 'c': 3}

    Why it's wrong here

    Modification during iteration raises error.

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.