PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A programmer is writing a function that accepts a dictionary of employee records and must return a tuple containing the number of records and the average salary. The function should not modify the input dictionary. Which function definition achieves this?
⚠ Common exam trap
The trap here is overlooking the empty-dictionary case or accidentally summing keys instead of values, which are common mistakes when working with dictionaries.
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
✓
def summarize(employees): count = len(employees) if count == 0: return (0, 0) total = sum(employees.values()) return (count, total / count)
The correct function must return a tuple with the count and average salary, avoid modifying the input, and handle an empty dictionary gracefully. The chosen option includes a check for zero count, returns a sensible default, and uses dictionary values for the salary sum. The others either fail on empty input, sum the wrong data, or mutate the input.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
def summarize(employees): count = len(employees) total = sum(employees.keys()) return (count, total / count)
Why it's wrong here
This function incorrectly sums the dictionary keys instead of the values. If the keys are employee IDs (likely strings or integers), summing them will either raise a TypeError (if strings) or produce a meaningless total. The average salary would be wrong. The function also lacks empty-dictionary handling, but the primary error is summing keys rather than values.
- ✗
def summarize(employees): count = len(employees) total = sum(employees.values()) return (count, total / count)
Why it's wrong here
This function returns a tuple with count and average, but it does not handle the case of an empty dictionary, which would cause a ZeroDivisionError. The scenario does not specify behavior for empty input, but a robust function should handle it. However, the primary issue is that it does not ensure the dictionary is not modified; it doesn't modify it, but the lack of empty handling makes it less ideal. The question asks for a function that achieves the goal; this one works for non-empty dictionaries but fails for empty ones.
- ✓
def summarize(employees): count = len(employees) if count == 0: return (0, 0) total = sum(employees.values()) return (count, total / count)
Why this is correct
This function correctly returns a tuple of the number of records and the average salary, and it handles the empty dictionary case by returning (0, 0). It does not modify the input dictionary. The use of employees.values() assumes that the dictionary values are numeric salaries, which aligns with the scenario. This is the most robust and correct implementation.
- ✗
def summarize(employees): count = len(employees) total = sum(employees.values()) employees.clear() return (count, total / count)
Why it's wrong here
This function modifies the input dictionary by calling employees.clear(), which violates the requirement that the input dictionary must not be modified. Although it returns the correct tuple for non-empty dictionaries, the side effect of clearing the dictionary is unacceptable. Additionally, it does not handle the empty dictionary case, which would cause a division by zero.
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.