Courseiva

PCEP Practice Question: Functions, Tuples, Dictionaries and Exceptions

A developer needs to determine the number of elements in a tuple named 't'. Which code snippet will correctly return the length?

⚠ Common exam trap

The PCEP exam often tests the immutability of tuples by presenting list-specific methods (like `append`, `pop`, or item assignment) as distractors, knowing that candidates may confuse tuples with 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

✓

len(t)

The correct way to determine the number of elements in a tuple in Python is to use the built-in `len()` function, which returns the length (number of items) of any sequence or collection, including tuples. Option C is correct because `len(t)` directly gives the count of elements in tuple `t`.

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.append(5)

    Why it's wrong here

    Tuples are immutable and expose no append method, so this raises AttributeError rather than returning a count; len(t) is required. It is tempting because append is the standard way to add elements to a list, making it the right call when building a mutable sequence.

  • ✗

    t[0] = 5

    Why it's wrong here

    Tuple item assignment raises TypeError because tuples are immutable, so no length is returned; len(t) is needed. It is tempting since indexed assignment is valid for lists, making it correct when updating an element of a mutable sequence.

  • ✓

    len(t)

    Why this is correct

    `len(t)` returns the tuple's element count directly, satisfying the requirement to determine how many items `t` holds. Tuples implement `__len__`, so `len()` works on them exactly as on lists or strings, yielding the integer length without modifying the immutable tuple.

  • ✗

    t.pop()

    Why it's wrong here

    Tuples provide no pop method, so this raises AttributeError instead of returning a count; len(t) is required. It is tempting because pop removes and returns the last element of a list, making it the right call when consuming items from a mutable stack.

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 →

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.