Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A developer is debugging a script that uses a `while` loop to process items from a queue. The loop is intended to run until the queue is empty, but the script hangs indefinitely. Which of the following is the most likely cause?

⚠ Common exam trap

The trap here is assuming that a `break` statement is necessary to exit a `while` loop, when actually the loop condition must become false.

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

✓

The loop condition never becomes false because the queue is not being modified inside the loop.

An infinite `while` loop occurs when the loop condition never evaluates to false. If the queue is not being emptied, the condition `while queue:` remains true. The loop body must include an operation that removes items from the queue, such as `pop()` or `popleft()`, to eventually make the condition false and terminate the loop.

Answer analysis

Option-by-option breakdown

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

  • ✗

    The `break` statement is missing from the loop body.

    Why it's wrong here

    While a `break` statement can exit a loop, it is not required if the loop condition is properly managed. The hang is due to the condition never becoming false, not the absence of `break`. Adding `break` without fixing the condition would still leave the loop uncontrolled.

  • ✗

    The loop uses `continue` instead of `break`.

    Why it's wrong here

    Using `continue` instead of `break` would not cause an infinite loop by itself; it would just skip the rest of the current iteration. The infinite loop arises from the condition not changing, regardless of `continue` usage. The core issue is the lack of queue modification.

  • ✓

    The loop condition never becomes false because the queue is not being modified inside the loop.

    Why this is correct

    If the queue is not modified within the loop (e.g., items are not removed), the condition `while queue:` remains true forever, causing an infinite loop. The loop must include an operation that eventually makes the condition false, such as `queue.pop()` or `queue.popleft()`, to terminate.

  • ✗

    The queue is a list, and lists cannot be used in `while` conditions.

    Why it's wrong here

    Lists can be used in `while` conditions; an empty list is falsy, and a non-empty list is truthy. The problem is not the data type but the failure to remove items from the list. Using a list is perfectly valid; the loop just needs to modify it.

About these practice questions

One of 482 original PCEP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.