Question 335 of 511
StringshardMultiple ChoiceObjective-mapped

Quick Answer

The correct answer is that the code raises a TypeError: 'str' object does not support item assignment. This happens because strings in Python are immutable, meaning once a string object is created, its individual characters cannot be changed in place. When you attempt to assign a new value to an index like `s[0] = 'x'`, the interpreter immediately rejects the operation at runtime, enforcing the immutability contract of the `str` type. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of Python’s core data type properties and the distinction between mutable and immutable objects. A common trap is confusing strings with lists, which do support item assignment; many beginners expect strings to behave the same way. To remember this, think of a string as a sealed envelope—you cannot erase and rewrite a single letter inside; you must create a whole new envelope.

PCAP Strings Practice Question

This PCAP practice question tests your understanding of strings. The scenario asks you to isolate a root cause — eliminate options that address a different problem before choosing. After answering, compare your reasoning against the explanation and wrong-answer breakdown below. Once you have made your selection, read the full explanation to reinforce the concept and understand why each distractor is designed to mislead on exam day.

A developer writes: s = 'abc'; s[0] = 'x'. What happens?

Question 1hardmultiple choice
Full question →

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

TypeError: 'str' object does not support item assignment

In Python, strings are immutable, meaning their contents cannot be changed after creation. Attempting to assign a new character to an index position (e.g., `s[0] = 'x'`) raises a `TypeError: 'str' object does not support item assignment`. This is a fundamental property of the `str` type in Python, enforced at the interpreter level.

Key principle: Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Answer analysis

Option-by-option breakdown

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

  • s becomes 'xbc'

    Why it's wrong here

    Strings do not support item assignment.

  • TypeError: 'str' object does not support item assignment

    Why this is correct

    This is the exact error raised.

    Related concept

    Read the scenario before looking for a memorised answer.

  • ValueError: string index out of range

    Why it's wrong here

    Index 0 is valid, so no IndexError; the error is TypeError about assignment.

  • s becomes 'abc' and no error

    Why it's wrong here

    An error is raised; the string remains unchanged.

Common exam traps

Common exam trap: answer the scenario, not the keyword

Python Institute often tests the immutability of strings by presenting an assignment to an index, tricking candidates who confuse strings with mutable sequences like lists.

Detailed technical explanation

How to think about this question

Under the hood, Python strings are stored as immutable sequences of Unicode code points in a compact internal structure (e.g., PyUnicodeObject). This immutability allows strings to be interned (shared) for efficiency and used as dictionary keys without risk of mutation. A real-world scenario is when processing large text data: attempting to modify a string character-by-character would be inefficient; instead, developers use `str.replace()`, slicing, or convert to a list of characters, modify, and join back.

KKey Concepts to Remember

  • Read the scenario before looking for a memorised answer.
  • Find the constraint that changes the correct option.
  • Eliminate answers that are true in general but not in this case.

TExam Day Tips

  • Watch for words such as best, first, most likely and least administrative effort.
  • Review why wrong options are wrong, not only why the correct option is correct.

Key takeaway

Answer the scenario, not the keyword: identify the specific constraint before choosing the most familiar-sounding option.

Real-world example

How this comes up in practice

A media company stores terabytes of video archives that are accessed once a year for audit purposes. Moving these objects to a cold storage tier (Azure Archive, S3 Glacier, or Google Nearline) costs a fraction of hot storage. Questions like this test whether you understand storage tiers, access frequency tradeoffs, and retrieval latency requirements.

What to study next

Got this wrong? Here's your next step.

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

Related practice questions

Related PCAP practice-question pages

Use these pages to review the topic behind this question. This is how one missed question becomes focused revision.

Practice this exam

Start a free PCAP practice session

Short sessions build daily habit. Longer sessions build exam-day stamina. Try a timed session to simulate real conditions.

FAQ

Questions learners often ask

What does this PCAP question test?

Strings — This question tests Strings — Read the scenario before looking for a memorised answer..

What is the correct answer to this question?

The correct answer is: TypeError: 'str' object does not support item assignment — In Python, strings are immutable, meaning their contents cannot be changed after creation. Attempting to assign a new character to an index position (e.g., `s[0] = 'x'`) raises a `TypeError: 'str' object does not support item assignment`. This is a fundamental property of the `str` type in Python, enforced at the interpreter level.

What should I do if I get this PCAP question wrong?

Identify which exam domain this question belongs to, review the core concept, then practise similar questions from the same domain.

What is the key concept behind this question?

Read the scenario before looking for a memorised answer.

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

Same concept, more angles

1 more ways this is tested on PCAP

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. A developer tries to modify a string: s = 'hello'; s[0] = 'H'. What happens when this code runs?

medium
  • A.It changes the string to 'Hello'
  • B.It raises a TypeError: 'str' object does not support item assignment
  • C.It creates a new string 'Hello' and assigns it to s
  • D.It raises an IndexError because index 0 is out of range

Why B: Option A is correct because strings are immutable; assigning to an index raises TypeError. Option B is wrong because it does not assign a new string; assignment to index fails. Option C is wrong because it raises an error, it does not succeed. Option D is wrong because the error is TypeError, not IndexError.

Keep practising

More PCAP practice questions

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