PCEP Computer Programming and Python Fundamentals Practice Question
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?
⚠ Common exam trap
Many candidates confuse `dict.get()` with `dict.setdefault()` or misread the condition logic (e.g., thinking `if not config.get('debug')` checks for key existence rather than truthiness of the value), leading them to pick options that either add the key when 'debug' is True or use methods that overwrite existing keys.
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
✓
if not config.get('debug'): config.setdefault('timeout', 30)
`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).
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
if not config.get('debug'): config.setdefault('timeout', 30)
Why this is correct
setdefault adds key only if missing, and condition checks debug is False.
- ✗
if config.get('debug') == False: config.update({'timeout': 30})
Why it's wrong here
update would overwrite existing 'timeout' key, not checking if it already exists.
- ✗
if config['debug']: config['timeout'] = 30 else: pass
Why it's wrong here
Adds timeout when debug is True, opposite of required.
- ✗
if config.setdefault('debug', False) == False: config['timeout'] = 30
Why it's wrong here
setdefault on 'debug' returns its current value (True), so condition false, and also adds 'debug' key if missing, which is not intended.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 498 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 →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
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.