PCAP Object-Oriented Programming Practice Question
A developer wants to ensure that an attribute 'balance' of a BankAccount class cannot be accessed directly from outside the class but can be accessed through a method. Which approach should be used?
⚠ Common exam trap
Python Institute often tests the distinction between single underscore (`_`) as a convention versus double underscore (`__`) as name mangling, and candidates mistakenly believe that a single underscore provides actual access restriction.
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
✓
Declare 'self.__balance' and provide a getter method
Prefixing the attribute with double underscores (`__balance`) triggers Python's name mangling, which renames the attribute to `_BankAccount__balance` at runtime. This prevents direct access from outside the class (e.g., `obj.balance` raises an `AttributeError`), while a getter method (e.g., `get_balance()`) can still retrieve the value. This is the standard Python idiom for achieving 'weak' private encapsulation in OOP.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
Declare 'self.balance' as a class variable
Why it's wrong here
Merely declaring balance as a class variable—via an assignment inside the class body—makes it a public attribute on the class, so any code can read or write it as ClassName.balance or instance.balance. Python does not distinguish between 'public' and 'private' class variables; without name mangling or a property, there is no mechanism to prevent direct access. Moreover, if instances also assign self.balance, that creates a per-instance attribute, but that too remains publicly accessible, so the requirement to ensure privacy is not met.
- ✗
Declare 'self.balance' and provide a getter method
Why it's wrong here
This approach provides a getter, but the underlying attribute is still called balance with no leading underscores and no name mangling, so callers can access and mutate it directly without ever invoking the getter. A getter is only useful when the attribute itself is hidden or at least non-writable by convention; here, the getter is redundant because the public attribute is already readable and writable. Since the developer specifically wants to ensure (not just suggest) privacy, relying on discipline to use the getter is insufficient.
- ✗
Declare 'self._balance' and provide a getter method
Why it's wrong here
Using self._balance marks the attribute as 'internal' by a widely followed naming convention, but Python's runtime does not enforce or conceal it. Direct access from outside is still successful: other code can read and assign _balance unless they explicitly choose to respect the underscore. The getter adds no real protection because the single-underscore prefix only signals intent to other programmers, not to the interpreter, so the attribute is not actually private.
- ✓
Declare 'self.__balance' and provide a getter method
Why this is correct
Assigning self.__balance inside the class triggers Python's name mangling: the attribute is stored as _ClassName__balance, making it impossible (or at least very awkward) for external code to access it by the original name. The getter then provides the only controlled public way to read its value, and a matching setter or property can validate changes before writing. This combination actually ensures encapsulation, which is why it meets the developer's requirement.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 421 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 →
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.