Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

What is the result of the following expression?

d = {'a': 1} d.get('b', 0)

⚠ Common exam trap

The PCEP exam often tests the distinction between `dict.get()` (which returns a default or `None`) and direct subscript access `d[key]` (which raises `KeyError`), trapping candidates who confuse the two behaviors.

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

✓

0

The `get()` method on a dictionary returns the value for the given key if it exists; otherwise, it returns the default value provided as the second argument. Since key `'b'` is not in dictionary `d`, the method returns `0` (the specified default). Option A is correct because `d.get('b', 0)` explicitly supplies a default of `0`.

Answer analysis

Option-by-option breakdown

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

  • ✓

    0

    Why this is correct

    dict.get returns the value for the given key, or the supplied default when the key is absent. Since 'b' is not in {'a': 1}, the method returns the default 0 rather than raising KeyError, leaving the dictionary unchanged.

  • ✗

    None

    Why it's wrong here

    dict.get returns the supplied default 0 when the key is absent, never None, so this misreads the method's fallback behaviour. It is tempting because None is the return value of get() when no default argument is passed, which is the correct result for d.get('b') alone.

  • ✗

    KeyError

    Why it's wrong here

    get() never raises KeyError for a missing key; it returns the default instead, unlike bracket subscripting such as d['b']. It is tempting because KeyError is exactly what d['b'] produces, so it is the right answer when the expression uses direct indexing rather than get().

  • ✗

    1

    Why it's wrong here

    The value 1 belongs to key 'a'; key 'b' is absent, so get() returns the default 0 rather than any stored value. It is tempting because 1 is the correct result for d.get('a', 0), where the requested key genuinely exists in the dictionary.

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 →

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.