PCEP Control Flow, Loops, Lists and Logic Practice Question
An inventory script stores product quantities in a list named stock. The script must print the running total of quantities from the start of the list up to, but not including, the first zero quantity it encounters. Which code fragment produces this behavior?
⚠ Common exam trap
The trap here is assuming that break and continue behave the same way inside a loop when they actually exit versus skip.
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 for q in stock: if q == 0: break total += q print(total)
The requirement is to accumulate quantities until the first zero appears, then stop without adding that zero. Placing the zero test before the addition and exiting with break achieves exactly this, because the loop terminates before the zero can be added to the running total.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
total = 0 for q in stock: total += q if q == 0: break print(total)
Why it's wrong here
Here the addition happens before the zero check, so the first zero is included in total. The running sum therefore stops at the zero instead of just before it, which contradicts the stated requirement of summing only the values preceding the first zero.
- ✓
total = 0 for q in stock: if q == 0: break total += q print(total)
Why this is correct
This loop adds each quantity to total and immediately exits with break when a zero is found, so only elements before the first zero are summed. Because the test occurs before the addition, the zero itself is never added, matching the requirement exactly.
- ✗
total = 0 for q in stock: if q == 0: continue total += q print(total)
Why it's wrong here
Using continue skips only the zero values but keeps iterating over the entire list, so every nonzero quantity after the first zero is also added. The resulting total is the sum of all nonzero entries, not the sum up to the first zero.
- ✗
total = 0 for q in stock: total += q if q == 0: continue print(total)
Why it's wrong here
This fragment adds every quantity, including zeros, and the continue statement has no effect because it is the last statement in the loop body. The total is simply the sum of the whole list, so it does not stop at the first zero at all.
Go deeper
Related to this question
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 →
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.