Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A teacher writes a program that must print each student name from a list called roster on its own line, but stop printing entirely as soon as the name "STOP" is encountered, without printing "STOP" itself. Which loop body achieves this?

⚠ Common exam trap

The trap here is assuming continue and break behave the same, when continue only skips one iteration instead of ending the loop.

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 name in roster: if name == "STOP": break print(name)

Placing the sentinel comparison before the print call and using break ensures the loop terminates at the first occurrence of "STOP" without emitting that value. Checking after printing would leak the sentinel into the output, and using continue would skip only one element while later names still print, so only the pre-check with break satisfies both conditions.

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 name in roster: while name != "STOP": print(name) break

    Why it's wrong here

    This nests a while loop whose condition is evaluated once per name, and the inner break only exits the while, not the for. When the name is "STOP" the while body never runs, but the outer for continues with later names, so printing does not actually stop at the sentinel as required.

  • ✓

    for name in roster: if name == "STOP": break print(name)

    Why this is correct

    The condition is checked before printing, so when STOP is reached the break statement exits the loop immediately and the print call is skipped for that iteration. Every name before STOP prints on its own line, and no further iterations run, exactly matching the teacher's requirement to halt without printing the sentinel value.

  • ✗

    for name in roster: print(name) if name == "STOP": break

    Why it's wrong here

    Because print runs before the check, the sentinel "STOP" is printed on its own line before the loop exits. The teacher explicitly asked that "STOP" not appear in the output, so this ordering violates the requirement even though it does stop iterating at the right moment afterward.

  • ✗

    for name in roster: if name == "STOP": continue print(name)

    Why it's wrong here

    The continue statement only skips the current iteration; the loop then proceeds to the next name and keeps printing everything after "STOP". The teacher wants printing to stop permanently at that point, so skipping a single element leaves all subsequent names in the output and fails the requirement.

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.