Courseiva

PCAP Object-Oriented Programming Practice Question

A developer wants a class 'Point' to have a readable string representation that returns 'Point(x, y)'. Which special method should be overridden?

⚠ Common exam trap

Python Institute often tests the distinction between `__repr__` and `__str__`, where candidates mistakenly choose `__str__` because they think 'readable' refers to user-friendly output, but the question's specific format 'Point(x, y)' is the classic `__repr__` pattern for unambiguous object representation.

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

✓

__repr__

The `__repr__` method is designed to return an unambiguous string representation of an object, often used for debugging and development. Overriding `__repr__` to return `'Point(x, y)'` fulfills the requirement for a readable string representation that matches the specified format. This method is called by the `repr()` built-in function and by the interactive interpreter when evaluating an expression.

Answer analysis

Option-by-option breakdown

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

  • ✓

    __repr__

    Why this is correct

    __repr__ is the correct method because it defines the canonical, unambiguous string representation of an object, which is used by the repr() function and the interactive interpreter. Its goal is to return a string that, ideally, can be passed to eval() to recreate the object, making it the most developer-friendly and readable representation for diagnostic purposes. Since the developer wants a readable string for a class, __repr__ is the recommended choice to implement.

  • ✗

    __format__

    Why it's wrong here

    __format__ is not appropriate here because it is only invoked when using format(obj, spec) or f-strings with a format specifier, such as f"{obj:>10}". It controls how an object is presented within a larger formatted string, not how the object appears when directly converted to a string via str() or repr(). Implementing __format__ alone has no effect on the default string representation produced by str() or repr().

  • ✗

    __str__

    Why it's wrong here

    While __str__ does produce a human-readable version of an object and is called by str() and print(), it is meant for informal, end-user-oriented output rather than an unambiguous developer representation. Since the question asks a developer for a readable string, the intended answer is the more exact and developer-focused __repr__, which also serves as the fallback if __str__ is not defined. In most debugging and REPL contexts, __repr__ is what actually gets displayed.

  • ✗

    __unicode__

    Why it's wrong here

    __unicode__ is a Python 2-only special method that returns a unicode string, and it has no effect in Python 3 because the str type now represents both byte strings and unicode text. Python 3 removed __unicode__ entirely, so defining it in a class would be ignored, and the developer must use __str__ or __repr__ instead. Relying on __unicode__ would be a common migration mistake and is definitely not the way to provide a readable string in modern Python.

About these practice questions

This PCAP question is part of Courseiva's 421-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 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.