Courseiva
Question 208 of 498
Functions, Tuples, Dictionaries and ExceptionshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

Which exception is raised when trying to access a dictionary key that does not exist?

⚠ Common exam trap

Python Institute often tests whether candidates confuse `KeyError` with `ValueError` or `TypeError`, especially when the question involves dictionary operations like `pop()` or `del` on a missing key, where the same `KeyError` is raised.

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

KeyError

In Python, when you attempt to access a dictionary key that does not exist using square bracket notation (e.g., `my_dict['nonexistent']`), the interpreter raises a `KeyError`. This is the standard exception for missing dictionary keys, as defined in the Python language specification. The correct answer is A.

Answer analysis

Option-by-option breakdown

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

  • KeyError

    Why this is correct

    Accessing a missing key raises KeyError.

  • TypeError

    Why it's wrong here

    TypeError is for operations on inappropriate types.

  • ValueError

    Why it's wrong here

    ValueError is for functions with inappropriate value.

  • AttributeError

    Why it's wrong here

    AttributeError is for invalid attribute references.

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

Same concept, more angles

2 more ways this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. A dictionary: d = {1: 'a', 2: 'b', 3: 'c'}. Which code will cause a KeyError?

hard
  • A.d.get(4)
  • B.if 4 in d: d[4]
  • C.d[4]
  • D.d.setdefault(4, 'd')

Why C: Accessing a dictionary key that does not exist using square bracket notation (d[4]) raises a KeyError. Since the dictionary d has keys 1, 2, and 3, the key 4 is absent, so d[4] triggers the error.

Variation 2. Refer to the exhibit. What is the output?

medium
  • A.Done
  • B.Success\nDone
  • C.Key missing\nDone
  • D.Key missing\nSuccess\nDone

Why C: The code attempts to access a dictionary key ('key') that does not exist, which raises a KeyError. The except block catches this specific exception and prints 'Key missing'. After the try-except, the 'finally' block (or code after the try-except) prints 'Done'. The output is therefore 'Key missing' followed by 'Done' on separate lines.

Last reviewed: Jun 30, 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.