Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer writes a function that takes a tuple as an argument and tries to modify an element inside the tuple. What happens?

⚠ Common exam trap

Python Institute often tests the distinction between `TypeError` and `ValueError` — the trap here is that candidates may confuse an operation that is not allowed (TypeError) with an operation that receives an invalid value (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

✓

The code raises a TypeError.

Tuples in Python are immutable, meaning their elements cannot be changed after creation. Attempting to modify an element (e.g., `my_tuple[0] = 5`) raises a `TypeError` because the tuple object does not support item assignment. This is a fundamental property of the tuple data type.

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 code raises a TypeError.

    Why this is correct

    Tuples are immutable, so any assignment to an index raises a TypeError at runtime. The interpreter blocks the modification rather than silently ignoring it, which satisfies the stem's scenario of attempting to change a tuple element.

  • ✗

    The tuple is converted to a list automatically.

    Why it's wrong here

    Tuples are immutable; no implicit conversion to list occurs on assignment, so the statement raises TypeError instead. The temptation is that list() genuinely converts a tuple, but that requires an explicit call producing a new object, not automatic behaviour during item assignment.

  • ✗

    The first element is modified successfully.

    Why it's wrong here

    Tuple item assignment always raises TypeError because tuples are immutable; no element is written. The temptation is that indexing reads work fine, so assignment looks symmetrical, but only mutable sequences such as lists permit item rebinding, and the tuple's stored references cannot be altered in place.

  • ✗

    The code raises a ValueError.

    Why it's wrong here

    Item assignment on a tuple raises TypeError, not ValueError; ValueError covers type-correct values that are invalid, such as int('abc'). The temptation is that both signal failed operations, yet the exception class here is fixed by Python's data model for unsupported assignment on an immutable sequence.

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

Senior Network & Security Engineer · founder of Courseiva

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.