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 dictionary key raises KeyError, satisfying the stem's requirement for the exception triggered by absent keys. Python's dict lookup invokes `__getitem__`, which raises KeyError rather than returning a default, unlike `get()`. This distinguishes it from IndexError, used by sequences such as lists and tuples.
- ✗
TypeError
Why it's wrong here
TypeError is raised when an operation is applied to an object of an inappropriate type, such as adding a string to an integer. It is tempting because a missing key can seem like a type mismatch, but dictionary subscript access raises KeyError, not TypeError.
- ✗
ValueError
Why it's wrong here
ValueError is raised when a function receives an argument of the right type but an inappropriate value, such as int('abc'). It is tempting because a missing key feels like a bad value, but dictionary subscript access raises KeyError, not ValueError.
- ✗
AttributeError
Why it's wrong here
AttributeError is raised when an attribute reference or assignment fails on an object, such as calling a non-existent method. It is tempting because dictionary keys are accessed with dot-like syntax in some languages, but Python dictionary lookups raise KeyError, not AttributeError.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 482 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
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.
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.