Courseiva

PCAP Object-Oriented Programming Practice Question

A class MyClass defines __str__ and __repr__ methods. What is the purpose of __repr__?

⚠ Common exam trap

Test-takers frequently confuse `__repr__` with `__str__`, assuming both serve the same purpose, but The PCAP exam specifically tests that `__repr__` is for unambiguous debugging output, not user-friendly display.

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

✓

To return an unambiguous representation of the object, ideally for debugging

The `__repr__` method is designed to return an unambiguous string representation of an object, ideally one that can be used to recreate the object or that clearly shows its internal state for debugging purposes. This is distinct from `__str__`, which targets end-user readability. The Python documentation explicitly states that `__repr__` should be unambiguous, while `__str__` should be readable.

Answer analysis

Option-by-option breakdown

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

  • ✗

    To return a human-readable string for end users

    Why it's wrong here

    Human-readable output for end users is the contract of __str__, which is called by str(obj) and print(obj). __repr__, by contrast, is meant to be a developer-facing representation that prioritizes unambiguity over aesthetics, so while repr output can also be readable, readability is not its purpose. Choosing this option would confuse the informal string representation with the official one.

  • ✗

    To compare object equality

    Why it's wrong here

    Equality between objects is decided by the __eq__ method (and its counterpart __ne__), which returns a boolean, not by __repr__, which returns a string. Two objects can have identical repr strings yet be unequal, because repr does not participate in comparison logic. This option therefore describes a completely different dunder method with a different return type and semantics.

  • ✗

    To return a hash value of the object

    Why it's wrong here

    A hash value is an integer produced by __hash__, used for dictionary and set lookups, whereas __repr__ returns a string representation of the object. The two methods have entirely separate contracts: hash output is a fixed-size numeric digest, while repr output is a descriptive, potentially multi-line string. Misidentifying repr as a hash generator would incorrectly assume its return type and its role in the object model.

  • ✓

    To return an unambiguous representation of the object, ideally for debugging

    Why this is correct

    The canonical purpose of __repr__ is to return an unambiguous, developer-oriented string that is ideally a valid Python expression such that eval(repr(obj)) recreates the object. This makes it the primary tool for debugging and logging, where the exact type and state of an object matter more than polished presentation. It intentionally favors completeness and precision over the informal readability that __str__ provides.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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 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.