PCEP Computer Programming and Python Fundamentals Practice Question
Which THREE of the following will correctly iterate over all keys and values of a dictionary d = {'a':1, 'b':2}?
⚠ Common exam trap
The PCEP exam often tests the distinction between iterating over keys, values, and items, and the trap here is that candidates may confuse `enumerate(d)` with `d.items()`, not realizing that `enumerate` adds an index rather than providing the dictionary's key-value pairs.
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
✓
for k in d: print(d[k])
Iterating over a dictionary directly with `for k in d` yields each key in turn, and `print(d[k])` then accesses the corresponding value. This is a standard and efficient way to iterate over both keys and values without creating intermediate objects.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
for i, k in enumerate(d):
Why it's wrong here
enumerate gives index, not key-value.
- ✓
for k in d: print(d[k])
Why this is correct
Default iteration over keys.
- ✗
for v in d.values(): print(v) -- only values
Why it's wrong here
Does not print keys.
- ✓
for k, v in d.items():
Why this is correct
Retrieves both key and value.
- ✓
for k in d.keys(): print(d[k])
Why this is correct
Accesses value via key.
Go deeper
Related to this question
About these practice questions
This PCEP question is part of Courseiva's 498-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 →
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.