Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

Which of the following will raise a TypeError?

⚠ Common exam trap

Python Institute often tests the distinction between mutable and immutable types, specifically that tuples cannot be modified after creation, while lists, sets, and dictionaries can be changed via their respective methods or assignments.

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

✓

t = (1, 2); t[0] = 3

Tuples are immutable in Python; attempting to assign a value to an element using indexing (e.g., `t[0] = 3`) raises a `TypeError` with the message 'tuple' object does not support item assignment. This is a fundamental property of the tuple data type, designed to create fixed sequences that cannot be changed after creation.

Answer analysis

Option-by-option breakdown

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

  • ✓

    t = (1, 2); t[0] = 3

    Why this is correct

    Assigning to an index of a tuple raises TypeError because tuples are immutable: their elements cannot be reassigned after creation. The statement `t[0] = 3` attempts item assignment on a tuple, which Python rejects outright, satisfying the stem's requirement for a TypeError rather than a silent success or a different exception.

  • ✗

    lst = [1, 2]; lst.extend([3, 4])

    Why it's wrong here

    list.extend accepts any iterable and appends its items, so passing a list succeeds silently. It is tempting because extend with a non-iterable such as an integer raises TypeError, but a list argument is precisely the intended usage.

  • ✗

    x = {1, 2} & {2, 3}

    Why it's wrong here

    Set intersection with & is defined for sets and returns the shared elements, so no exception occurs. It is tempting because & on a list and an integer raises TypeError, but here both operands are sets, making the operation valid.

  • ✗

    d = {'a': 1}; d['b'] = 2

    Why it's wrong here

    Assigning to a new key in a dictionary inserts the pair without error. It is tempting because indexing a missing key for reading raises KeyError, but assignment creates the entry, so no TypeError arises here.

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.