PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A Python script uses a dictionary to store user session data. The developer writes `user = {'id': 101, 'name': 'Alice'}` and later tries to access `user['email']`. What is the outcome?
⚠ Common exam trap
Python Institute often tests the distinction between direct bracket access (which raises KeyError) and the `.get()` method (which returns None or a default), tempting candidates to think Python automatically returns a falsy value for missing keys.
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
✓
It raises a KeyError.
In Python, accessing a dictionary key that does not exist raises a KeyError. The dictionary `user` contains only the keys 'id' and 'name', so `user['email']` triggers a KeyError because the key 'email' is not present. This is a fundamental behavior of Python dictionaries, which do not return default values for missing keys unless a method like `.get()` is used.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
It returns an empty string.
Why it's wrong here
A missing dictionary key raises KeyError rather than yielding an empty string; only string methods and similar types return empty values. It is tempting because empty-string defaults are common in configuration parsing, where code uses get() with a fallback — but plain subscripting has no such default.
- ✓
It raises a KeyError.
Why this is correct
Accessing a missing key with subscript notation raises KeyError, because dictionaries do not return a default for absent keys. The stem's `user` dictionary holds only `'id'` and `'name'`, so `user['email']` fails immediately at runtime rather than returning `None`.
- ✗
It returns None.
Why it's wrong here
Subscripting a missing key raises KeyError; None is returned only by dict.get() when the key is absent. It is tempting because None is Python's conventional absence marker and get() is the idiomatic safe lookup, so developers expect that behaviour — but the bracket syntax does not use get().
- ✗
It checks the 'in' operator automatically and returns False.
Why it's wrong here
Subscript access on a missing key raises KeyError; Python does not silently invoke the 'in' operator or return False. It is tempting because membership testing with 'in' does return a boolean, and that is the correct way to check key presence before access — but the subscript expression itself never performs that check.
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 →
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.