Courseiva
Question 316 of 498
Data Types, Variables, Basic I/O and OperatorshardMultiple ChoiceObjective-mapped

PCEP Practice Question: Data Types, Variables, Basic I/O and Operators

Exhibit

Refer to the exhibit.

config = {
    "debug": False,
    "port": 8080,
    "host": "localhost"
}

print(config.get("timeout", "Not set"))

What is the output of the following code?

config = {}

print('Not set' if config.get('timeout') is None else config.get('timeout'))

⚠ Common exam trap

Python Institute often tests the distinction between dict[key] (which raises KeyError) and dict.get(key) (which returns None) to trap candidates who assume all dictionary access methods behave the same way.

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

Not set

The code attempts to access a dictionary key 'timeout' that does not exist. Using square bracket access on a missing key raises a KeyError, but the code uses the .get() method, which returns None by default if the key is missing. Since the key 'timeout' is not in the dictionary, .get('timeout') returns None, and the print statement outputs 'Not set' because the condition `config.get('timeout') is None` evaluates to True.

Answer analysis

Option-by-option breakdown

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

  • KeyError: 'timeout'

    Why it's wrong here

    KeyError is raised only with bracket access, not using .get() method.

  • Not set

    Why this is correct

    The .get() method returns None for missing key, so the condition is True, outputting 'Not set'.

  • None

    Why it's wrong here

    The .get() method returns None by default when the key is missing, but the code prints 'Not set' because the condition checks for None and is True.

  • False

    Why it's wrong here

    The condition evaluates to True (since .get() returns None), so it prints 'Not set', not False.

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

1 more way 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 system administrator is automating server configuration using Python. She has a dictionary: config = {'host': 'localhost', 'port': 8080, 'debug': True}. She needs to add a new key 'timeout' with value 30 if it does not already exist, but only if the 'debug' key is False. If 'debug' is True, she should not add 'timeout'. Additionally, she wants to ensure that the ordering of keys in the dictionary remains stable (insertion order). Which code snippet correctly implements this logic?

hard
  • A.if not config.get('debug'): config.setdefault('timeout', 30)
  • B.if config.get('debug') == False: config.update({'timeout': 30})
  • C.if config['debug']: config['timeout'] = 30 else: pass
  • D.if config.setdefault('debug', False) == False: config['timeout'] = 30

Why A: `config.get('debug')` returns `True` (the value of the 'debug' key), and `not True` evaluates to `False`, so the `if` block is not entered — thus 'timeout' is not added. If 'debug' were `False`, `not False` would be `True`, and `config.setdefault('timeout', 30)` would add the key only if it did not already exist, preserving insertion order (Python 3.7+ guarantees dict order).

Last reviewed: Jun 30, 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.