PCEP Control Flow, Loops, Lists and Logic Practice Question
A developer is building a simple inventory check. The list stock holds integers representing item quantities. He wants to append the string 'low' to a separate list alerts once for each quantity that is less than 5. Which code correctly populates alerts?
⚠ Common exam trap
The trap here is placing break after the append, which looks like it just stops checking but actually prevents any later low quantities from being recorded.
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
✓
for q in stock: if q < 5: alerts.append('low')
Appending inside a loop with a conditional adds one entry per qualifying quantity, which matches the requirement of one 'low' per item below 5. Breaking after the first match stops too early, using any collapses the count to a single Boolean decision, and rebinding alerts with a comprehension replaces rather than extends the existing list.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
for q in stock: if q < 5: alerts.append('low')
Why this is correct
This loop visits every quantity in stock, tests whether it is below 5, and appends 'low' to alerts each time the test succeeds. Because the append is inside the if but inside the loop, the string is added once per qualifying quantity, and the number of entries in alerts matches the count of low quantities, which is exactly what the developer needs.
- ✗
if any(q < 5 for q in stock): alerts.append('low')
Why it's wrong here
The any function returns a single Boolean indicating whether at least one quantity is below 5. Appending once based on that result adds at most one 'low' entry, even if many items are low. This answers whether any low item exists, not how many, so it does not produce one alert per qualifying quantity as required.
- ✗
alerts = ['low' for q in stock if q < 5] appended to the existing alerts list.
Why it's wrong here
This comprehension builds a new list of 'low' strings, one per qualifying quantity, which is the right count. However, assigning it to alerts replaces the existing list rather than extending it, and there is no append of a list here. If the developer intends to add to the current alerts contents, this rebinding discards any prior entries, so it does not correctly populate the existing list.
- ✗
for q in stock: if q < 5: alerts.append('low') break
Why it's wrong here
Adding break after the append causes the loop to end at the first quantity below 5. Only one 'low' entry would ever be added, regardless of how many low quantities exist later in the list. The scenario requires one entry per qualifying quantity, so terminating early undercounts the alerts and fails to reflect the full inventory.
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.