PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A developer writes a function that attempts to access a dictionary key that may not exist: def get_value(data, key): try: return data[key] except KeyError: return None. They call it as get_value({'a': 1}, 'b'). What is the result and why?
⚠ Common exam trap
The trap here is forgetting that the except block handles the exception, so no error is raised to the caller.
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
✓
None is returned because the KeyError is caught and handled.
The function uses a try-except block to handle a potential KeyError. When the key 'b' is not found, the exception is caught, and the except block returns None. This provides a safe way to access dictionary values without causing the program to crash. The caller receives None as the result of the function call.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The function returns 1 because it falls back to the first available key.
Why it's wrong here
There is no fallback to other keys in the code. The function only attempts to access the specified key 'b'. If that key is missing, it returns None. It does not search for other keys or return the first value in the dictionary. This option invents a behavior that is not present in the function definition.
- ✗
The function returns the string 'KeyError' because the exception is converted to a string.
Why it's wrong here
The except block does not convert the exception to a string; it simply returns None. The exception object itself is not returned or used. This option confuses exception handling with string conversion, which would require explicit code like str(e). The function's behavior is to return None, not the exception's name or message.
- ✓
None is returned because the KeyError is caught and handled.
Why this is correct
When data[key] is evaluated and the key 'b' is not found, a KeyError is raised. The except KeyError block catches this specific exception and executes its body, which returns None. The function then exits normally. This demonstrates how exception handling can provide a fallback value instead of crashing. The caller receives None as the result.
- ✗
A KeyError is raised because the key 'b' is not in the dictionary.
Why it's wrong here
The KeyError is caught by the except KeyError block, which returns None. The exception does not propagate to the caller because it is handled inside the function. This option ignores the presence of the try-except construct. The function is designed to gracefully handle missing keys by returning a default value, so no exception is raised to the caller.
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 and reviewed by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
Last reviewed September 2026 · checked against the official Python Institute exam blueprint
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.