Courseiva

PCEP Computer Programming and Python Fundamentals Practice Question

A QA engineer needs to run a test 5 times. Which loop construct is most appropriate?

⚠ Common exam trap

Many candidates confuse the `while` loop (option B) as equally valid, but the PCEP exam expects knowledge that `for` loops with `range` are the preferred and most Pythonic construct for fixed-count iteration, while `while` loops are intended for condition-based repetition where the number of iterations is not known in advance.

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 i in range(5): ...

The `for i in range(5)` loop is the most idiomatic and concise way to repeat an action a fixed number of times (5 iterations) in Python. The `range(5)` generates a sequence from 0 to 4, and the loop body executes exactly 5 times, which directly matches the requirement to run a test 5 times without needing manual counter management.

Answer analysis

Option-by-option breakdown

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

  • ✗

    do: ... while counter < 5

    Why it's wrong here

    Python provides no do-while syntax; the construct is invalid and raises SyntaxError before execution. It is tempting because do-while guarantees at least one execution, which suits menus or retry prompts where the body must run before the condition is checked.

  • ✗

    while counter < 5: ... counter += 1

    Why it's wrong here

    A while loop suits indefinite iteration, where the repetition count is unknown until a condition changes at runtime. Here the count is fixed at five, so a for loop over range(5) expresses that directly without manual counter initialisation or increment. The while form would be the right choice when looping until a sentinel value appears.

  • ✓

    for i in range(5): ...

    Why this is correct

    range(5) yields exactly five values, so the for loop body executes precisely five times without a manually managed counter. A while loop would need separate increment logic, making this construct the clearest fit for a fixed repetition count.

  • ✗

    def repeat(): ... repeat()

    Why it's wrong here

    Recursion not needed.

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

Same concept, more angles

1 more way this is tested on PCEP

These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.

Variation 1. How many times will the following loop print 'Hi'? for i in range(3): print('Hi')

easy
  • A.2
  • ✓ B.3
  • C.0
  • D.4

Why B: The loop `for i in range(3):` iterates exactly three times because `range(3)` generates the sequence 0, 1, 2. Each iteration executes `print('Hi')`, so 'Hi' is printed three times. Option B is correct.

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.