PCAP Object-Oriented Programming Practice Question
A class 'MyClass' has a method 'do_something' that uses 'self.__private'. A subclass 'MySubClass' tries to access 'self.__private' and gets an AttributeError. Why?
⚠ Common exam trap
Python Institute often tests the misconception that double underscore attributes are truly private and not inherited, when in fact they are inherited under a mangled name, and the error arises from the subclass attempting to access the unmangled name.
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
✓
Because name mangling renames the attribute to _MyClass__private, and the subclass implicitly accesses _MySubClass__private
Python's name mangling mechanism renames any attribute prefixed with double underscores (like `__private`) in a class definition to `_ClassName__private`. When `MySubClass` tries to access `self.__private`, Python looks for `_MySubClass__private`, which does not exist, causing an AttributeError. The attribute `_MyClass__private` is still accessible from the subclass, but only via its mangled name.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Because the attribute is defined as a class attribute, not instance attribute
Why it's wrong here
Name mangling rewrites every identifier of the form __private that appears in a class body, regardless of whether it is assigned in __init__ or directly in the class statement. Defining the attribute as a class attribute does not change the substitution: in MyClass it becomes _MyClass__private, while the subclass's reference to __private becomes _MySubClass__private. The AttributeError is caused by the nonexistence of that subclass-mangled name, not by the attribute being a class-level rather than an instance-level storage.
- ✗
Because the subclass overrides the method that uses the attribute
Why it's wrong here
If the subclass overrides a method that uses __private, the overriding method is compiled in the subclass namespace, so its self.__private access is rewritten to self._MySubClass__private. Overriding the method does not create that attribute, nor does it affect the original base-class method's own mangled reference, which remains _MyClass__private and is inherited and usable inside the base-class method. Thus the override is irrelevant to the AttributeError; the problem is purely the textual name transformation that occurs in the subclass.
- ✓
Because name mangling renames the attribute to _MyClass__private, and the subclass implicitly accesses _MySubClass__private
Why this is correct
In Python, any attribute name written with two leading underscores inside class MyClass is transformed at compile time to _MyClass__private, a process called name mangling. When the same spelling __private appears in the body of a subclass, the compiler uses the subclass's name, producing _MySubClass__private; since that attribute was never assigned, self.__private raises AttributeError. The base class value still exists, but only under the base-mangled key _MyClass__private.
- ✗
Because the attribute is private and not inherited
Why it's wrong here
Python has no truly private members; double underscores only trigger name mangling, not inheritance blocking. The attribute _MyClass__private is an ordinary attribute of the instance and is inherited by subclass objects, so it is accessible from outside or from base-class code as _MyClass__private. It is not inherited as __private, because the subclass textually mangles that spelling to its own class prefix, making the private marker a source-code renaming rather than an access-control boundary.
Go deeper
Related to this question
About these practice questions
One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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.