Courseiva
Question 462 of 169
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

PCAP Object-Oriented Programming Practice Question

A Python developer is implementing a class that should behave like a sequence and support indexing. Which pair of special methods must be defined to achieve this?

⚠ Common exam trap

Python Institute often tests the distinction between the sequence protocol (__getitem__ + __len__) and the iterator protocol (__iter__ + __next__), trapping candidates who think iteration alone enables indexing.

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

__getitem__ and __len__

To make a class behave like a sequence and support indexing (e.g., obj[0]), Python requires the __getitem__ method to retrieve items by key. Additionally, the __len__ method is needed to define the length of the sequence, which is used by built-in functions like len() and is part of the sequence protocol. Together, these two methods satisfy the minimal requirements for a sequence-like object that supports indexing.

Answer analysis

Option-by-option breakdown

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

  • __getitem__ and __contains__

    Why it's wrong here

    Adding `__contains__` enables the `in` operator but does not provide positional access; the sequence protocol minimally requires `__len__` for `len()` and `__getitem__` for indexing. A container can be fully sequence-like for reading without any membership override, because `__contains__` is optional and falls back to a linear scan when absent. Replacing `__len__` with `__contains__` leaves the object unable to report its size or support index-based access, so it fails the core sequence contract.

  • __setitem__ and __delitem__

    Why it's wrong here

    `__setitem__` and `__delitem__` only handle mutation operations (`obj[i] = value` and `del obj[i]`), so they belong to the mutable-sequence interface, not the read-only sequence protocol. Without `__getitem__` and `__len__`, the object has no way to fetch a value by position or report a length, making even basic indexing impossible. Adding mutation methods cannot substitute for the two read-access dunders required for sequence behavior.

  • __iter__ and __next__

    Why it's wrong here

    `__iter__` and `__next__` create an iterator protocol that supports `for` loops and `next()`, but they do not make integer subscription work; `obj[i]` is handled by `__getitem__`, not by iteration. In fact, if `__getitem__` is present, Python can iterate by calling it with increasing indices until `IndexError`, so `__iter__` is not even required for iteration. The absence of `__len__` and `__getitem__` means the object cannot be measured with `len()` nor positionally indexed, so it is not a sequence.

  • __getitem__ and __len__

    Why this is correct

    According to the Python data model, a sequence is defined primarily by having a `__len__` method and a `__getitem__` method that accepts integer indices (and optionally slices). Together they give the object a finite length, support `obj[i]` indexing, and implicitly enable iteration via the old-style `__getitem__` fallback. On top of these two, the `collections.abc.Sequence` ABC automatically mixes in `__contains__`, `__iter__`, `__reversed__`, `index`, and `count`, showing how much behavior is derived from just these two methods.

About these practice questions

Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

Last reviewed: Jun 30, 2026

Question Discussion

Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.

Loading comments…

Sign in to join the discussion.

This PCAP 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 PCAP exam.