Courseiva

PCEP Control Flow, Loops, Lists and Logic Practice Question

A program reverses a string using a while loop. The code is:

text = "hello" reversed_text = "" index = len(text) - 1

while index > 0:

reversed_text += text[index] index -= 1

print(reversed_text)

It prints 'olle' instead of 'olleh'. What is the error?

⚠ Common exam trap

Python Institute often tests off-by-one errors in while loops, where candidates mistakenly think `index > 0` covers all elements because they forget that the first index is 0, not 1.

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

✓

Change the condition to 'while index >= 0:'

The while loop condition `index > 0` stops when `index` becomes 0, so the character at index 0 (the first character 'h') is never appended to `reversed_text`. Changing the condition to `while index >= 0:` ensures the loop runs for index values from 4 down to 0 inclusive, producing the full reversed string 'olleh'.

Answer analysis

Option-by-option breakdown

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

  • ✓

    Change the condition to 'while index >= 0:'

    Why this is correct

    The loop stops before processing index 0, so the character `'h'` is never appended. Changing the condition to `while index >= 0:` lets the final iteration run, appending `text[0]` and producing `'olleh'`. The off-by-one boundary in the loop guard is the sole defect.

  • ✗

    Change the initial index to len(text)

    Why it's wrong here

    len(text) equals 5, which is out of range and would raise an IndexError, not omit a character. It is tempting because starting at the length seems natural, and it would be correct if Python indices began at 1.

  • ✗

    Use a for loop instead

    Why it's wrong here

    A for loop is an alternative construct, not the cause of the missing final character. It is tempting because for loops avoid manual index decrementing, and it would be correct when iterating over a sequence without needing explicit index control.

  • ✗

    Use string slicing: reversed_text = text[::-1]

    Why it's wrong here

    Slicing replaces the loop entirely rather than fixing the boundary condition, so it does not explain the error. It is tempting because text[::-1] correctly reverses a string, and it would be the right approach if rewriting the program for brevity.

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 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.