PCEP Computer Programming and Python Fundamentals Practice Question
You are maintaining a Python script that calculates team bonuses based on sales data. The script reads a dictionary where keys are employee names and values are total sales (float). It then applies a 10% bonus if sales exceed 5000. The code snippet is:
def calculate_bonus(sales):
for name, value in sales.items():
if value > 5000:
print(f"{name} gets bonus")However, the manager wants the script to return a list of employees who qualify, not just print them. They also want to avoid side effects. What is the best way to modify this function?
⚠ Common exam trap
The PCEP exam often tests the concept of side effects versus pure functions, and the trap here is that candidates may think mutating the input dictionary (Option C) or using a global variable (Option A) are acceptable, when in fact they violate the principle of avoiding side effects and reduce code maintainability.
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
✓
Build a list inside the function and return it at the end.
It modifies the function to build a list of qualifying employee names inside the function and returns that list. This avoids side effects (no global variables, no mutation of the input dictionary) and follows the principle of returning results rather than printing them, making the function reusable and testable.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Create a global list variable at the top of the script and append each qualifying name to it.
Why it's wrong here
Global variables introduce side effects and reduce reusability.
- ✗
Keep the function as is and have the caller capture the printed names by redirecting stdout.
Why it's wrong here
Relying on printed output for data is fragile and not recommended.
- ✗
Use the dictionary's update method to mark bonus status in the original sales dictionary.
Why it's wrong here
Modifying the input dictionary is a side effect.
- ✓
Build a list inside the function and return it at the end.
Why this is correct
Returning a new list keeps the function pure and reusable.
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.