Question 62 of 169
PCAP Object-Oriented Programming Practice Question
Exhibit
Refer to the exhibit.
Traceback (most recent call last):
File "test.py", line 10, in <module>
print(obj.__private_attr)
AttributeError: 'MyClass' object has no attribute '__private_attr'
Given that MyClass defines __private_attr in __init__, why does this error occur?Given that MyClass defines __private_attr in __init__, why does this error occur?
⚠ Common exam trap
The Python Institute often tests the misconception that double underscores create truly private attributes, leading candidates to choose 'Private attributes cannot be accessed outside the class' when the real issue is name mangling and the attribute still being accessible via the mangled 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
✓
The attribute name is mangled to _MyClass__private_attr.
Python uses name mangling for attributes with double underscores (__) to avoid accidental overriding in subclasses. When you define __private_attr inside __init__, Python internally renames it to _MyClass__private_attr. Attempting to access obj.__private_attr from outside the class fails because that mangled name is not recognized, leading to an AttributeError.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
The attribute name is mangled to _MyClass__private_attr.
Why this is correct
Python applies name mangling to any identifier of the form __spam (with at most one trailing underscore) that occurs inside a class definition, rewriting it to _ClassName__spam. So self.__private_attr declared in __init__ is stored as self._MyClass__private_attr by the compiler. From outside the class, you must use that mangled name; accessing __private_attr directly will fail because no such attribute exists under that literal name.
- ✗
The attribute was not defined in __init__.
Why it's wrong here
The statement that the attribute was not defined in __init__ is contradicted by the question's premise: it was explicitly assigned as self.__private_attr inside __init__. If the attribute had never been created, any reference to it would raise an AttributeError, not a name-mangling issue. The entire confusion here stems from the transformed lookup, not from a missing initialization.
- ✗
Private attributes cannot be accessed outside the class.
Why it's wrong here
The phrase 'cannot be accessed' overstates the effect of name mangling: the attribute remains fully accessible from outside the class through the mangled name, e.g., obj._MyClass__private_attr. Name mangling is a compile-time identifier transformation designed to prevent accidental name collisions in subclasses, not a runtime access-control mechanism like Java's private. Python deliberately does not enforce privacy at the language level, so this option's absolute claim is inaccurate.
- ✗
The attribute is a class attribute not an instance attribute.
Why it's wrong here
Because the assignment occurs on an instance object via self.__private_attr = value, it creates a per-instance attribute stored in the instance's __dict__, not a class-level attribute. Class attributes are defined directly in the class body, not inside __init__ or on self. Name mangling affects the attribute's stored key, but it does not turn an instance attribute into a class attribute.
About these practice questions
Courseiva creates original exam-style practice questions with explanations and wrong-answer analysis. It does not publish real exam questions, exam dumps, or protected exam content. Learn why practice questions differ from exam dumps →
Last reviewed: Jul 4, 2026
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.
Question Discussion
Share a tip, memory trick, or ask about the reasoning behind this question. Do not post real exam questions, leaked content, braindumps, or copyrighted exam material. Comments are moderated and may be removed without notice.
Sign in to join the discussion.