Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

Exhibit

Refer to the exhibit.

s = "hello world"
try:
    print(s[100])
except IndexError:
    print("Index out of range")

What will the above code output?

⚠ Common exam trap

The PCAP exam often tests the distinction between IndexError and ValueError, trapping candidates who confuse out-of-range indexing with invalid value operations, such as int('abc') which raises ValueError.

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

✓

Index out of range

The code attempts to access an index that is outside the valid range of the string. In Python, strings are zero-indexed, so for a string of length n, valid indices are 0 to n-1. Accessing an index equal to or greater than the length raises an IndexError, which is exactly what 'Index out of range' describes.

Answer analysis

Option-by-option breakdown

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

  • ✓

    Index out of range

    Why this is correct

    The statement attempts to access an element using an index that is equal to or greater than the list's length, or a negative index less than -len(list). Python raises IndexError for any out-of-range sequence access, and the active except IndexError block catches this exact exception, printing the string 'Index out of range'. Since the handler successfully intercepts the exception, the program continues normally after the except block, making this the emitted output.

  • ✗

    The program runs without output.

    Why it's wrong here

    If the index were valid or the operation otherwise succeeded, the try block would finish without raising, the except block would be skipped, and the program would produce no output. However, the code deliberately forces an out-of-range access, so an exception is guaranteed to occur. Consequently, control transfers to the except IndexError handler, which prints a message—meaning the program does not run silently. The presence of a triggered except block always produces the handler's output in this scenario.

  • ✗

    The program crashes with an unhandled IndexError.

    Why it's wrong here

    An unhandled IndexError occurs only when no matching except clause catches it or the try/except structure is absent. Here, the code includes an except IndexError block that is specifically designed to catch this exception type. Once the exception is raised, Python matches it against the handler, executes the print statement, and does not propagate the exception to the calling stack. Therefore, the program does not crash; it completes normally after the handler runs.

  • ✗

    ValueError is raised.

    Why it's wrong here

    ValueError is raised for operations that receive an argument of the correct type but an inappropriate value, such as int('abc') or removing a non-existent element from a list. In contrast, accessing a list with an integer index outside the valid range triggers IndexError, not ValueError. The exception type is determined by the operation being performed—sequence indexing with an invalid position always yields IndexError. Thus, the except block for IndexError catches it, and the program never raises ValueError.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 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 PCAP 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 PCAP exam.