Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer is writing a function that retrieves a value from a nested dictionary based on a sequence of keys. If any key is missing, the function should raise a custom exception MissingKeyError with the missing key. The function signature is def get_nested(data, keys):. Which implementation correctly meets these requirements?

⚠ Common exam trap

The trap here is using dict.get() and checking for None, which conflates missing keys with keys that have None values, or forgetting to pass the missing key to the custom exception.

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

✓

def get_nested(data, keys): for key in keys: if key not in data: raise MissingKeyError(key) data = data[key] return data

The correct implementation must check each key's presence, raise MissingKeyError with the missing key, and traverse the nested structure. The chosen option does exactly that using the 'in' operator and passes the key to the exception. The others either omit the key in the exception, mishandle None values, or raise the exception class without arguments.

Answer analysis

Option-by-option breakdown

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

  • ✓

    def get_nested(data, keys): for key in keys: if key not in data: raise MissingKeyError(key) data = data[key] return data

    Why this is correct

    This implementation iterates through the keys, checks if each key exists in the current dictionary, and raises MissingKeyError with the missing key if not. It then descends into the nested dictionary. This correctly handles missing keys at any level and returns the final value. It uses the 'in' operator to check membership, which is efficient for dictionaries.

  • ✗

    def get_nested(data, keys): for key in keys: data = data.get(key) if data is None: raise MissingKeyError(key) return data

    Why it's wrong here

    This implementation uses data.get(key), which returns None if the key is missing. However, if a key exists but its value is None, it would incorrectly raise MissingKeyError. The requirement is to raise an error only when the key is missing, not when the value is None. This is a subtle bug that could cause false positives.

  • ✗

    def get_nested(data, keys): for key in keys: if key not in data: raise MissingKeyError data = data[key] return data

    Why it's wrong here

    This implementation raises MissingKeyError without passing the missing key as an argument. The requirement explicitly states that the exception should include the missing key. While the logic for traversing the nested dictionary is correct, the exception is not instantiated with the key, so the error message would lack critical information.

  • ✗

    def get_nested(data, keys): try: for key in keys: data = data[key] return data except KeyError: raise MissingKeyError

    Why it's wrong here

    This implementation catches KeyError but raises MissingKeyError without specifying which key was missing. The requirement is to raise MissingKeyError with the missing key. Additionally, it does not preserve the original traceback context, but the primary failure is not providing the missing key. It also does not check for missing keys before attempting access, relying on exceptions, which is less explicit.

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 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.