Courseiva

PCAP Object-Oriented Programming Practice Question

A developer wants to implement a read-only property for a class 'Temperature' that returns the temperature in Celsius but prevents external modification. Which code snippet correctly defines such a property?

⚠ Common exam trap

Python Institute often tests the misconception that a property without a setter is still writable by default, or that overriding `__setattr__` is a valid alternative to `@property` for creating a read-only attribute, but the correct approach is to omit the setter decorator entirely.

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

✓

class Temperature: @property def celsius(self): return self._celsius

It defines a read-only property using the `@property` decorator with only a getter method. Without a setter, any attempt to assign a value to `celsius` will raise an `AttributeError`, making the property read-only. This is the standard Pythonic way to implement a read-only attribute.

Answer analysis

Option-by-option breakdown

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

  • ✗

    class Temperature: @property def celsius(self): return self._celsius @celsius.setter def celsius(self, val): self._celsius = val

    Why it's wrong here

    This version pairs a getter with an explicit @celsius.setter, so the property is fully writable. External code can freely assign to `celsius`, which defeats the intended read-only constraint. A read-only property must omit the setter method entirely, allowing only the getter to exist.

  • ✗

    class Temperature: def __setattr__(self, name, val): if name == 'celsius': raise AttributeError

    Why it's wrong here

    Overriding `__setattr__` to raise `AttributeError` for the name `'celsius'` is an ad-hoc hack that does not create a property descriptor. It also blocks legitimate internal assignments to `self.celsius` wherever they occur, and it does not provide the clean attribute-access behavior expected of a descriptor. This approach is neither idiomatic nor equivalent to a read-only property.

  • ✓

    class Temperature: @property def celsius(self): return self._celsius

    Why this is correct

    This is the correct read-only property because it defines only a getter method via `@property`. With no setter, Python treats the property as read-only, and assigning to `celsius` raises `AttributeError`. Internal storage in `self._celsius` remains private, while the property exposes a controlled public interface.

  • ✗

    class Temperature: def __init__(self, c): self.celsius = c

    Why it's wrong here

    In this option, `celsius` is just a regular instance attribute assigned in `__init__`. It has no getter or setter, so it is fully writable and offers no validation or protection. This is not a read-only property; it is a simple public attribute.

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.