Courseiva
Question 162 of 498
Control Flow, Loops, Lists and LogicmediumMultiple ChoiceObjective-mapped

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

    This ensures index 0 is processed.

  • Change the initial index to len(text)

    Why it's wrong here

    That would cause an IndexError.

  • Use a for loop instead

    Why it's wrong here

    Does not fix the specific bug.

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

    Why it's wrong here

    A valid alternative but not the error in the code.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

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.