Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

Exhibit

>>> s = "Hello"
>>> s[1] = "a"

Refer to the exhibit. What happens when the code is executed?

⚠ Common exam trap

Python Institute often tests the immutability of strings by presenting code that attempts index assignment, trapping candidates who assume strings are mutable like lists.

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

✓

A TypeError is raised

The code attempts to modify a string by assigning a new character to an index position (s[0] = 'H'). Strings in Python are immutable, meaning their elements cannot be changed after creation. This operation raises a TypeError because the item assignment is not supported for string objects.

Answer analysis

Option-by-option breakdown

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

  • ✗

    The string becomes "Hallo"

    Why it's wrong here

    String literals are immutable, so no in-place character substitution occurs; the original binding remains unchanged unless reassigned. The exhibit performs no such reassignment, so the value never becomes "Hallo". This tempts candidates who assume indexing assignment mutates strings like lists, which is the scenario where this would hold.

  • ✓

    A TypeError is raised

    Why this is correct

    The code combines incompatible operand types, such as concatenating a string with an integer or calling an unsupported operation on a value, so the interpreter halts and raises a TypeError rather than producing output or a different exception class.

  • ✗

    A SyntaxError is raised

    Why it's wrong here

    A SyntaxError arises only from malformed grammar, such as a missing colon or unbalanced parenthesis, which the parser detects before execution. The exhibit's code is syntactically valid, so it runs and fails at runtime instead. Candidates pick this when they spot unusual-looking constructs and assume the parser rejects them.

  • ✗

    The code runs without error and s remains "Hello"

    Why it's wrong here

    The code modifies the string through a mechanism that leaves the original object unchanged, so s still references "Hello" only if no reassignment occurred; where the exhibit reassigns or mutates via a builder, the value differs. It is tempting because strings are immutable, which would be correct if the code performed no reassignment.

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.