Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A logistics team stores shipment data as `shipment = {'id': 'A17', 'weight_kg': 12.5, 'destination': 'Osaka'}`. A developer needs to retrieve the destination but wants the program to keep running and supply the string 'UNKNOWN' if the key is ever missing from a shipment record. Which statement should the developer use?

⚠ Common exam trap

The trap here is assuming that subscript access combined with a logical fallback is equivalent to a default-returning lookup, when in fact the missing key raises KeyError before any fallback logic runs.

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

✓

shipment.get('destination', 'UNKNOWN')

Reading a dictionary value without risking a KeyError requires a method that accepts a default, and dict.get is the standard tool for that. It returns the stored value when the key is present and the supplied fallback when it is not, without altering the dictionary. Methods that insert or remove keys change the record's contents, which the scenario does not permit.

Answer analysis

Option-by-option breakdown

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

  • ✗

    shipment['destination'] or 'UNKNOWN'

    Why it's wrong here

    Indexing with a missing key raises KeyError immediately, and the `or` fallback is never reached because the exception interrupts evaluation. Even when the key exists, a legitimately empty string value would be replaced by 'UNKNOWN', which corrupts valid data. This does not give the safe, non-crashing behavior the logistics team requires for unreliable records.

  • ✓

    shipment.get('destination', 'UNKNOWN')

    Why this is correct

    The dict.get method returns the value mapped to the key when the key exists, and it returns the second argument as a default when the key is absent, so missing keys never raise an error. In this scenario it yields 'Osaka' for the current record and 'UNKNOWN' for any record lacking a destination, letting the program continue processing shipments.

  • ✗

    shipment.pop('destination', 'UNKNOWN')

    Why it's wrong here

    pop removes the key from the dictionary and returns its value, so a successful lookup would delete the destination from the shipment record entirely. The team wants to keep the data intact for later reporting. Using pop also makes repeated reads inconsistent, since the first call succeeds and subsequent calls return the default instead of the real value.

  • ✗

    shipment.setdefault('destination', 'UNKNOWN')

    Why it's wrong here

    setdefault does return 'UNKNOWN' for a missing key, but it also permanently inserts the key 'destination' with that value into the dictionary. That mutates the shipment record, so a later lookup can no longer distinguish a genuinely missing destination from one that was never supplied. The task only asks for a safe read, not a modification of the stored data.

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.