Courseiva
Question 132 of 498
Functions, Tuples, Dictionaries and ExceptionseasyMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

Exhibit

Refer to the exhibit.

config = {
    'host': 'localhost',
    'port': 8080,
    'timeout': 30
}

try:
    value = config['debug']
except KeyError:
    print('Key not found')

What is the output of the code?

⚠ Common exam trap

The PCEP exam often tests the distinction between `dict[key]` (which raises `KeyError`) and `dict.get(key, default)` (which returns the default), trapping candidates who expect an exception or assume the default is `None`.

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

Key not found

The code uses a dictionary's `get()` method with a default value of 'Key not found'. When the key 'b' is not present in the dictionary `d`, `get()` returns the specified default instead of raising an exception. Therefore, the output is 'Key not found', making option D correct.

Answer analysis

Option-by-option breakdown

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

  • AttributeError

    Why it's wrong here

    AttributeError is not raised because the code uses the get() method, which does not raise an exception for missing keys.

  • The program exits with no output

    Why it's wrong here

    The program does produce output because get() returns a default value.

  • None

    Why it's wrong here

    The default value is not None because the get() call specifies 'Key not found' as the default.

  • Key not found

    Why this is correct

    The get() method returns the default value 'Key not found' when the key is absent, so this is printed.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jul 4, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.