Courseiva

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

    A global list still mutates shared state across calls, so it does not avoid side effects and complicates testing. It is tempting because appending to an external list is a common beginner pattern, and it would be correct if the function were required to accumulate results across multiple invocations.

  • ✗

    Keep the function as is and have the caller capture the printed names by redirecting stdout.

    Why it's wrong here

    Redirecting stdout captures printed text, not structured data, and still leaves the function performing I/O side effects, violating the no-side-effects requirement. It is tempting because print already emits qualifying names, but stdout capture is for logging or CLI output, not for returning a typed list to a caller.

  • ✗

    Use the dictionary's update method to mark bonus status in the original sales dictionary.

    Why it's wrong here

    Mutating the input dictionary with update introduces exactly the side effect the manager wants removed, and it returns no list. It is tempting because update is a familiar way to record derived state, and it would be correct if the requirement were to persist bonus flags alongside the original sales figures.

  • ✓

    Build a list inside the function and return it at the end.

    Why this is correct

    Building a list inside the function and returning it satisfies both constraints: it replaces printing with a returned value, and it avoids side effects by keeping all state local. Appending each qualifying name during iteration, then returning the list, gives the manager the required collection without mutating external state.

About these practice questions

Courseiva writes every PCEP question from scratch — 482 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 →

How Courseiva writes practice questions · Editorial policy

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.