Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A quality engineer tracks defect counts per shift in a list called defects. She needs the running total of all defects after every shift, producing a list of cumulative sums with the same length as defects. Which approach produces that cumulative list?

⚠ Common exam trap

The trap here is placing the append before the addition, which yields a list that is shifted by one and never reaches the final total.

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

✓

total = 0 cumulative = [] for d in defects: total += d cumulative.append(total)

A running total requires an accumulator initialized before the loop, updated with each element, and recorded after the update. Appending after adding yields a cumulative value for every shift and keeps the output length equal to the input length. Appending before adding shifts all values by one position, and recomputing the whole-list sum each iteration produces a repeated grand total instead.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    cumulative = [] for d in defects: cumulative.append(sum(defects))

    Why it's wrong here

    Calling sum on the entire defects list inside the loop appends the same grand total once per element, so every entry in cumulative is identical and equals the total of all shifts. The engineer wants a running total that grows shift by shift, not a repeated final value, and the repeated full-list summation is also unnecessarily expensive.

  • ✗

    total = 0 cumulative = [] for d in defects: cumulative.append(total) total += d

    Why it's wrong here

    Appending before adding means the first entry is always zero and every subsequent entry lags one shift behind the true running total. The final total of all defects is never recorded because the last addition occurs after the last append, so the produced list is off by one position throughout.

  • ✗

    cumulative = [defects[i] + defects[i+1] for i in range(len(defects))]

    Why it's wrong here

    This expression adds each element to its neighbor rather than accumulating a running total, and on the final index i+1 is out of range, raising an IndexError. Even if the bounds were fixed, adjacent-pair sums are not cumulative sums, so the output would not represent the defect total after each shift.

  • ✓

    total = 0 cumulative = [] for d in defects: total += d cumulative.append(total)

    Why this is correct

    The accumulator total starts at zero, and each iteration adds the current defect count before appending the updated total to the new list. Because the append happens after the addition, the resulting list holds the cumulative sum at each shift and has exactly the same length as the defects list, which is what the engineer needs.

About these practice questions

This PCEP question is part of Courseiva's 482-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 →

How Courseiva writes practice questions · Editorial policy

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.