Courseiva
Question 442 of 498
Functions, Tuples, Dictionaries and ExceptionseasyMultiple ChoiceObjective-mapped

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A support technician is running a Python script that parses a configuration file and stores key-value pairs in a dictionary called 'config'. The script then uses these values to set application parameters. The configuration file is optional, and some expected keys may be missing. Currently, the script crashes with a KeyError when accessing a missing key. The technician needs to modify the script to safely retrieve a value or return 'N/A' if a key is missing. The script must remain efficient and readable. Which modification best achieves this?

⚠ Common exam trap

The PCEP exam often tests the distinction between `dict.get()` and `dict.setdefault()`, trapping candidates who think `setdefault()` is a safe retrieval method without realizing it permanently modifies the dictionary by adding the missing key.

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

Use config.get(key, 'N/A') instead of direct key access

`dict.get(key, default)` is the idiomatic Python method for safely retrieving a value from a dictionary without raising a `KeyError`. It returns the default value `'N/A'` when the key is missing, which directly solves the crash while keeping the code concise and readable. This approach is more efficient than exception handling or explicit membership checks because it performs a single hash lookup.

Answer analysis

Option-by-option breakdown

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

  • Use config.get(key, 'N/A') instead of direct key access

    Why this is correct

    The get() method returns the specified default if key is missing, avoiding exceptions.

  • Wrap each access in a try-except block to catch KeyError and assign 'N/A'

    Why it's wrong here

    While functional, this approach is verbose and slower than using get().

  • Use if key in config: value = config[key] else: value = 'N/A'

    Why it's wrong here

    This works but is more verbose and performs two dictionary lookups (one for 'in' and one for access).

  • Use config.setdefault(key, 'N/A') before accessing

    Why it's wrong here

    setdefault() adds the default to the dictionary, which may be undesirable if you don't want to modify the dictionary.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Same concept, more angles

3 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 student = {'name': 'John', 'age': 20}. To safely get the grade with a default of 'N/A', which code should be used?

easy
  • A.student.get('grade', 'N/A')
  • B.student['grade']
  • C.student.fetch('grade', 'N/A')
  • D.student['grade'] or 'N/A'

Why A: The `get()` method of a dictionary safely retrieves the value for a given key, returning a default value (here `'N/A'`) if the key does not exist. This avoids raising a `KeyError` when the key `'grade'` is missing from the dictionary.

Variation 2. What is the result of the following expression? d = {'a': 1} d.get('b', 0)

easy
  • A.0
  • B.None
  • C.KeyError
  • D.1

Why A: The `get()` method on a dictionary returns the value for the given key if it exists; otherwise, it returns the default value provided as the second argument. Since key `'b'` is not in dictionary `d`, the method returns `0` (the specified default). Option A is correct because `d.get('b', 0)` explicitly supplies a default of `0`.

Variation 3. What is the output of the code?

easy
  • A.AttributeError
  • B.The program exits with no output
  • C.None
  • D.Key not found

Why D: The code uses a dictionary's `get()` method with a default value of 'Key not found'. When the key 'b' is not present in the dictionary `d`, `get()` returns the specified default instead of raising an exception. Therefore, the output is 'Key not found', making option D correct.

Last reviewed: Jul 4, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.