Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer needs to store configuration settings as key-value pairs and later retrieve the value for a key that may not exist, providing a default value if the key is missing. Which dictionary method should be used?

⚠ Common exam trap

The trap here is assuming that setdefault() is equivalent to get() for retrieval, ignoring that setdefault() inserts the missing key into the dictionary, which may cause unintended side effects.

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

✓

get()

The get() method safely retrieves a value for a key and returns a default if the key is absent, without altering the dictionary. Other methods either modify the dictionary or do not directly retrieve values, making them unsuitable for a read-only lookup with a fallback.

Answer analysis

Option-by-option breakdown

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

  • ✓

    get()

    Why this is correct

    The get() method returns the value for a given key if it exists, otherwise it returns a specified default value (or None if no default is provided). It does not raise an exception for missing keys, making it ideal for this scenario.

  • ✗

    pop()

    Why it's wrong here

    pop() removes the key and returns its value, raising a KeyError if the key is missing unless a default is provided. Using it would mutate the dictionary, which is not desired for a simple retrieval operation, and it still requires handling the missing key.

  • ✗

    keys()

    Why it's wrong here

    keys() returns a view of all keys in the dictionary. It does not retrieve a value for a specific key and cannot provide a default. Using it would require additional logic to check membership and then access the value, which is more complex than needed.

  • ✗

    setdefault()

    Why it's wrong here

    setdefault() also returns a default if the key is missing, but it additionally inserts the key with that default into the dictionary. The scenario only requires retrieval with a default, not modification of the dictionary, so setdefault() has an unwanted side effect.

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 →

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.