Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A log file processing script uses a while loop to read lines until a specific pattern is found. The code currently hangs. The developer suspects an infinite loop. Which change is most likely to fix the issue?

⚠ Common exam trap

Python Institute often tests the misconception that adding a `break` statement is the universal fix for infinite loops, when in fact the root cause is usually a missing update to the loop condition variable, not the absence of an explicit exit command.

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

✓

Ensure the condition variable is updated inside the loop

An infinite while loop typically occurs when the condition controlling the loop never becomes false. In a log file processing script, the condition variable (e.g., a line counter or a flag indicating the pattern was found) must be updated inside the loop body. Without that update, the loop condition remains true indefinitely, causing the hang. Adding a `break` statement (option A) would exit the loop unconditionally after the first iteration, which is not the intended fix for a missing update.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Add a break statement after reading the line

    Why it's wrong here

    A break after reading executes only once per line, so it cannot terminate the loop on the pattern match; the loop still never exits if the pattern is absent. Break statements are for early loop exit on a condition, which is the right tool when the terminating condition is separate from the while test.

  • ✗

    Increase the sleep time in the loop

    Why it's wrong here

    Sleeping longer only delays each iteration; if the loop's condition never becomes false, it still never terminates. It is tempting because sleep is often used to throttle polling loops. The actual defect is a condition or read that never advances, which must be corrected instead.

  • ✗

    Replace while with for loop

    Why it's wrong here

    Swapping while for for changes iteration syntax, not the terminating condition; a for loop over the same unbroken sequence hangs identically. For loops suit bounded iteration over a known sequence, which is the correct choice when the number of iterations is fixed in advance.

  • ✓

    Ensure the condition variable is updated inside the loop

    Why this is correct

    An infinite while loop typically occurs when the controlling condition variable never changes, so the exit condition is never met. Updating that variable inside the loop body ensures the condition eventually becomes false, allowing termination.

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.