PCAP Object-Oriented Programming Practice Question
Exhibit
Refer to the exhibit.
```python
class Base:
def __init__(self):
self._x = 10
def get_x(self):
return self._x
class Derived(Base):
def __init__(self):
self._x = 20
obj = Derived()
print(obj.get_x())
```What is the output of the code?
⚠ Common exam trap
Python Institute often tests the distinction between attribute shadowing and method inheritance, specifically that a derived class can override an attribute without calling the base class constructor, leading to unexpected values when inherited methods access that attribute.
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
✓
20
The `Derived` class inherits the `get_x` method from the `Base` class, which returns `self._x`. When `obj.get_x()` is called, `self` refers to the `Derived` instance, and `self._x` accesses the `_x` attribute set in `Derived.__init__` (value 20). The `_x` attribute in `Derived` shadows the one in `Base`, so the output is 20.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
20
Why this is correct
The correct output is 20 because Derived defines its own __init__ method that executes instead of Base.__init__. Inside Derived.__init__, the statement self._x = 20 creates an instance attribute _x bound to the integer 20. When get_x is called, Python's method resolution order finds get_x on Base, and that method returns self._x, which resolves to the Derived instance's attribute, giving 20.
- ✗
AttributeError: 'Derived' object has no attribute 'get_x'
Why it's wrong here
get_x is inherited from Base. In Python, instance methods are looked up on the class of the object and its parent classes through the MRO, so a Derived object can call get_x exactly as if it were defined directly in Derived. There is no reason for an AttributeError; the method exists in Base and is fully accessible from Derived.
- ✗
10
Why it's wrong here
This option would only be correct if Base.__init__ had run and set self._x to 10. However, Derived.__init__ overrides Base.__init__ and does not call super().__init__(), so the base class initializer is never executed. The only assignment to _x is the one in Derived.__init__, which sets it to 20, making the returned value 20, not 10.
- ✗
AttributeError: 'Derived' object has no attribute '_x'
Why it's wrong here
The _x attribute is created in Derived.__init__ before get_x is ever called. When __init__ executes self._x = 20, Python stores _x in the instance's __dict__, so later attribute lookup for _x succeeds. There is no AttributeError because the instance clearly possesses the attribute, and the get_x method simply reads that existing attribute.
Go deeper
Related to this question
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 →
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.