PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions
A developer wants to use a tuple to store the names of the months. They attempt to change an element: months = ("Jan","Feb","Mar"); months[1] = "Februar". What is the result?
⚠ Common exam trap
The PCEP exam often tests the distinction between mutable (list) and immutable (tuple) types, and the trap here is that candidates confuse the immutability error with a ValueError or AttributeError, or assume tuples can be modified 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
Tuples in Python are immutable, meaning their elements cannot be changed after creation. Attempting to assign a new value to an index of a tuple (e.g., months[1] = "Februar") raises a TypeError, not a ValueError or AttributeError. This is because the assignment operation is not supported for tuple 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 tuple is updated to ("Jan","Februar","Mar")
Why it's wrong here
Tuples are immutable, so item assignment raises TypeError and the tuple remains unchanged. It is tempting because lists behave exactly this way, and the syntax months[1] = "Februar" is valid for lists; only the tuple type prevents the mutation.
- ✗
A ValueError is raised
Why it's wrong here
Item assignment on a tuple raises TypeError, not ValueError; ValueError arises from type-correct operations with invalid values, such as int("abc"). It is tempting because both are common built-in exceptions, but immutability violations map to TypeError.
- ✗
An AttributeError is raised
Why it's wrong here
Assigning to a tuple index raises TypeError, not AttributeError; AttributeError occurs when accessing a missing attribute such as months.append. It is tempting because both are Python exceptions raised at runtime, but the tuple's immutability triggers the item-assignment TypeError specifically.
- ✓
A TypeError is raised
Why this is correct
Tuples are immutable sequences, so item assignment is unsupported. Attempting `months[1] = "Februar"` invokes the tuple's missing `__setitem__` behaviour, raising `TypeError: 'tuple' object does not support item assignment`. This satisfies the stem's constraint that the element change cannot succeed, unlike a list, which permits such mutation.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCEP question from scratch — 482 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 →
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.