PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A developer needs to count how many times each word appears in a list named words. The code starts with counts = {} and then iterates over the list. Which loop body correctly increments the count for each word, creating the key when it is first seen?
⚠ Common exam trap
The trap here is assuming that counts[word] += 1 initialises a missing key to zero automatically, when in fact it raises KeyError on the first occurrence.
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
✓
counts[word] = counts.get(word, 0) + 1
Counting occurrences requires reading a possibly missing key without raising an exception and then storing the incremented value. The get method supplies a fallback of 0 for absent keys, and assigning the sum back to the dictionary both creates and updates the entry. The other forms either raise KeyError on first sight of a word or misuse a dictionary method.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
counts[word] += 1
Why it's wrong here
Augmented assignment reads the current value of counts[word] before adding one. On the first occurrence of a word the key does not exist, so the read raises KeyError and the loop terminates. This approach only works when every key has already been initialised, which is not the case here, so it fails to build the counts dictionary from an empty starting point.
- ✗
counts.setdefault(word) + 1
Why it's wrong here
setdefault(word) returns the existing value or the default None, and adding 1 to None raises TypeError. Even if a default of 0 were supplied, the returned integer would not be stored back into the dictionary, so the count would never be updated. The statement also discards its result, making it useless for accumulating frequencies.
- ✓
counts[word] = counts.get(word, 0) + 1
Why this is correct
The get method returns the current count if the key exists and 0 otherwise, so the expression always produces a valid integer to increment. Assigning the result back to counts[word] creates the key on first encounter and updates it thereafter. This one-line idiom avoids a KeyError and correctly accumulates frequencies for every distinct word in the list.
- ✗
counts.update(word, counts[word] + 1)
Why it's wrong here
dict.update accepts a mapping or an iterable of key-value pairs, not two separate arguments, so this call raises TypeError. Additionally, counts[word] would raise KeyError on the first occurrence. The method also merges rather than assigning a single computed value, so it is the wrong tool for maintaining a running per-word tally.
Go deeper
Related to this question
About these practice questions
One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
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.