Courseiva

PCAP Object-Oriented Programming Practice Question

Which TWO of the following are valid ways to define a property in a Python class? (Select exactly 2.)

⚠ Common exam trap

Python Institute often tests the distinction between defining a property via the `property()` function or `@property` decorator versus overriding dunder methods like `__getattr__` or `__getattribute__`, which are attribute interception hooks, not property definitions.

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

✓

Use `property(getter, setter)` as a class variable.

`property(getter, setter)` is a built-in function that returns a property object, which can be assigned as a class variable to define a managed attribute. Option E is correct because the `@property` decorator is the standard, concise way to define a read-only property by decorating a method that acts as the getter.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Define a method named `property` inside the class.

    Why it's wrong here

    Defining a method named `property` inside a class merely creates a regular method attribute; it does not invoke Python's built-in `property` type. To construct a property descriptor you must pass getter and setter functions to the `property` callable or use the `@property` decorator at class definition time. Naming a method `property` can also shadow the built-in within the class namespace, but it never produces descriptor behavior.

  • ✗

    Override `__getattribute__` and check the attribute name.

    Why it's wrong here

    Overriding `__getattribute__` intercepts every attribute lookup on the instance, so you would need to implement the descriptor logic manually and manage per-name dispatch for all attributes, not just one. This global hook is a low-level customization that also risks infinite recursion if you access attributes carelessly inside the override. A proper property, by contrast, is an isolated descriptor that encapsulates getter/setter behavior for a single named attribute.

  • ✗

    Override `__getattr__` to simulate a property.

    Why it's wrong here

    The `__getattr__` method is a fallback hook called only when normal attribute lookup fails, so it cannot define a true property descriptor; it merely simulates behavior for missing names. Using it to fake a property requires separate handling of `__setattr__` and `__delattr__` for write protection and deletion semantics, and it does not honor descriptor precedence. A real property uses the descriptor protocol to centralize access logic for exactly one attribute, including optional setters and deleters.

  • ✓

    Use `property(getter, setter)` as a class variable.

    Why this is correct

    Assigning `property(getter, setter)` as a class variable directly creates a data descriptor that manages a specific attribute. Because `property` is a class implementing `__get__` and `__set__`, Python calls the supplied getter and setter functions whenever the attribute is accessed or assigned. This functional form is valid and is equivalent to the decorator form, though less commonly used in modern code.

  • ✓

    Use the @property decorator on a method.

    Why this is correct

    Applying the `@property` decorator to a method turns that method into a getter, making a read-only attribute accessed without parentheses. It is syntactic sugar for `property(method)`, and you can later add a setter with `@attr.setter` and a deleter with `@attr.deleter`. This is the idiomatic way to define properties in Python and clearly signals that a method should expose computed or validated state as an attribute.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 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 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.