Courseiva
Object-Oriented ProgramminghardMultiple ChoiceObjective-mapped

PCAP Object-Oriented Programming Practice Question

Which of the following is a correct use of the @property decorator to create a getter and setter for an attribute named 'score' that ensures score stays between 0 and 100?

⚠ Common exam trap

The PCAP exam often tests the distinction between using the property name itself (causing recursion) versus a private backing attribute, and the requirement that the setter must include validation logic to satisfy constraints like range checks.

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

@property def score(self): return self._score @score.setter def score(self, value): if 0 <= value <= 100: self._score = value

It uses the @property decorator to define a getter method that returns the private attribute `self._score`, and a setter method that validates the new value is between 0 and 100 before assigning it to `self._score`. This ensures encapsulation and data validation, which is the intended use of properties in Python.

Answer analysis

Option-by-option breakdown

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

  • @property def _score(self): return self.score @_score.setter def _score(self, value): self.score = value

    Why it's wrong here

    The `_score` property's getter returns `self.score`, not `self._score`, so it never reads the private backing field every access to `self.score` from inside the getter triggers another attribute lookup. If `score` is also a property that reads or writes `_score`, the two accessors call each other endlessly and cause infinite recursion; if `score` is an unmanaged attribute, the property logic is bypassed entirely. Thus this setup fails because the property does not isolate its own storage attribute.

  • @property def score(self): return self.score @score.setter def score(self, value): self.score = value

    Why it's wrong here

    The getter is decorated with `@property` on `score`, but it returns `self.score`, which is exactly the same property name. Consequently, every read of `self.score` re-enters the getter, and every assignment in the setter re-enters the setter, producing unbounded recursion and eventually a `RecursionError`. A property must delegate to a distinct internal attribute, conventionally `_score`, to break this self-referential lookup.

  • def get_score(self): return self._score def set_score(self, value): self._score = value score = property(get_score, set_score)

    Why it's wrong here

    `property(get_score, set_score)` is a legitimate functional API and the getter/setter do use a private `_score` field, so this approach avoids recursion and works as a basic property. However, the setter assigns `self._score = value` unconditionally, accepting any value outside the required 0–100 range. Because the question explicitly asks for range enforcement, this is a valid property but does not meet the validation requirement.

  • @property def score(self): return self._score @score.setter def score(self, value): if 0 <= value <= 100: self._score = value

    Why this is correct

    This is the canonical property pattern: the getter returns the private `_score` attribute, and the setter validates the incoming `value` before assigning it to `_score`. By using the backing field rather than the public property name, the code avoids recursion and gives the property exclusive control over reads and writes. When the validation condition fails, the setter silently refuses to update, keeping `_score` unchanged and enforcing the 0–100 range.

About these practice questions

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