PCAP Object-Oriented Programming Practice Question
A junior developer wrote a class representing a bank account with a private attribute balance. They used double underscore prefix (__balance) to make it private. However, in a test script, they are still able to access the attribute using the mangled name _Account__balance. The developer is confused about why encapsulation is not enforced. Which statement best explains this behavior?
⚠ Common exam trap
The trap here is that Python Institute often tests the misconception that double underscore prefix enforces true privacy like in languages such as Java or C++, when in reality Python's name mangling is merely a renaming convention that does not prevent access from outside the class.
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
✓
Python's name mangling is only a convention and does not prevent access.
Python's name mangling (triggered by a double underscore prefix) is not a security mechanism but a syntactic transformation that renames the attribute to _ClassName__attribute. This prevents accidental name clashes in subclasses but does not enforce true encapsulation; the attribute can still be accessed via the mangled name from outside the class. The developer's confusion stems from mistaking name mangling for a privacy guarantee, which Python intentionally does not provide.
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 double underscore prefix actually makes the attribute completely inaccessible from outside the class.
Why it's wrong here
In Python, a `__name` attribute is not made completely inaccessible; name mangling rewrites it to `_ClassName__name`. The attribute remains in the instance's `__dict__` and can be read or modified using that predictable mangled name. Therefore, the double underscore prefix does not create a true privacy barrier.
- ✓
Python's name mangling is only a convention and does not prevent access.
Why this is correct
Name mangling is a syntactic transformation, not a security mechanism: `self.__balance` inside a class becomes `self._BankAccount__balance`. It does not prevent external code from accessing the attribute via that mangled name, so it is only a convention. It primarily helps avoid accidental overrides in subclasses rather than hiding data.
- ✗
The developer forgot to use the @property decorator.
Why it's wrong here
The `@property` decorator is a tool for defining getter/setter/delete behavior on attributes, not a way to make them private. If it were omitted, the attribute would simply be directly accessible; adding `@property` would not block access unless paired with an explicitly raising setter. Forgetting `@property` is unrelated to whether a test can access or modify a balance field.
- ✗
The test script must have used a different class attribute name.
Why it's wrong here
The test script does not need a different attribute name because the mangled name `_BankAccount__balance` is the same underlying attribute and is deterministic. Using that name gives direct access, while any other name would raise an `AttributeError`. The mangling transformation does not create a second attribute; it only renames the original in a predictable way.
Go deeper
Related to this question
About these practice questions
This PCAP question is part of Courseiva's 169-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.