Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer defines a tuple `config = ('localhost', 8080, True)`. Later, the code attempts `config[1] = 9090`. What happens when this line executes?

⚠ Common exam trap

The trap here is assuming that because lists allow item assignment, tuples might allow it under certain conditions, such as matching types or automatic conversion.

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 because tuple objects do not support item assignment.

Tuples are immutable, meaning their elements cannot be changed after the tuple is created. The statement config[1] = 9090 tries to assign to an index, which triggers a TypeError. No automatic rebinding or type-based exception occurs. To change a value, you must build a new tuple and assign it to the variable.

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 tuple is updated in place, and config becomes ('localhost', 9090, True).

    Why it's wrong here

    Tuples are immutable, so they cannot be modified after creation. Attempting to assign to an index raises a TypeError. The tuple remains unchanged; no in-place update occurs. This option incorrectly assumes tuple immutability can be bypassed by index assignment, which is not allowed in Python.

  • ✗

    The assignment succeeds only if the new value is of the same type as the old value.

    Why it's wrong here

    Type consistency does not affect tuple immutability. Even assigning an integer to an integer position, as in config[1] = 9090, raises TypeError. Tuples never support item assignment, regardless of the types involved. This option invents a rule that does not exist in Python.

  • ✗

    A new tuple is created automatically with the updated value, and config is rebound to it.

    Why it's wrong here

    Python does not automatically create a new tuple when you try to assign to an element. Index assignment on a tuple always raises TypeError. Rebinding would require explicit code such as config = config[:1] + (9090,) + config[2:]. The scenario shows a direct assignment, which fails.

  • ✓

    A TypeError is raised because tuple objects do not support item assignment.

    Why this is correct

    Tuples are immutable sequences. Any attempt to assign to an index, such as config[1] = 9090, raises TypeError with a message like 'tuple' object does not support item assignment. The tuple itself remains unchanged. This is the correct behavior for the given code.

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 and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official Python Institute exam blueprint

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.