AlgoMaster Logo

Encapsulation - Quiz

Last Updated: January 3, 2026

1 min read

Encapsulation Exercises

29 quizzes

1
Code Completion

Access the current balance of the account using the provided method instead of touching internal state directly.

python
1
current = account.()

Click an option to fill the blank:

2
Code Completion

Store a new salary value using the public method instead of assigning directly to the internal attribute.

python
1
employee.(75000)

Click an option to fill the blank:

3
Code Completion

Read a mangled attribute of the Logger class by using its internal transformed name.

python
1
print(logger._Loggersecret_token.)

Click an option to fill blank 1 of 2:

4
Multiple Choice

What does encapsulation mainly help you achieve in a Python class?

5
Multiple Choice

In Python, what is the default access level of a class attribute without underscores?

6
Multiple Choice

Which attribute name most clearly signals a protected attribute by convention?

7
Multiple Choice

Why might you mark an attribute as private using double underscores in a base class?

8
Multiple Choice

In a banking app, which design best respects encapsulation for updating an account balance?

9
Multiple Choice

What is the internal name of attribute __token defined in class Session?

10
Multiple Choice

How should external code typically access a private attribute that stores configuration?

11
Multiple Choice

Which statement about protected members is most accurate in Python?

12
Multiple Choice

In a subclass, how do you normally access a protected attribute _config defined in the base class?

13
Multiple Choice

Which option best describes a public method in an encapsulated class?

14
Multiple Choice

If both Parent and Child define __value attributes, what does name mangling ensure?

15
Sequencing

Order the steps to design an encapsulated BankAccount class and use it to deposit money.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to safely use name-mangled attributes to avoid a clash between Parent and Child.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code?

1class BankAccount:
2    def __init__(self, owner, balance=0):
3        self.owner = owner
4        self.__balance = balance
5    def get_balance(self):
6        return self.__balance
7
8acct = BankAccount('Dana', 150)
9print(acct.get_balance())
18
Output Prediction

What is the output of this code involving a protected attribute?

1class Vehicle:
2    def __init__(self, make):
3        self._make = make
4
5class Car(Vehicle):
6    def full_name(self):
7        return f'Car: {self._make}'
8
9c = Car('Tesla')
10print(c.full_name())
19
Output Prediction

What is the output of this code using name mangling?

1class Parent:
2    def __init__(self):
3        self.__label = 'parent'
4
5class Child(Parent):
6    def __init__(self):
7        super().__init__()
8        self.__label = 'child'
9
10ch = Child()
11print(ch._Parent__label)
20
Output Prediction

What does this code print when accessing a name-mangled attribute?

1class Service:
2    def __init__(self, api_key):
3        self.__api_key = api_key
4
5svc = Service('XYZ')
6print(hasattr(svc, '_Service__api_key'))
21
Output Prediction

What is the output of this code that mixes public and private attributes?

1class User:
2    def __init__(self, username):
3        self.username = username
4        self.__status = 'active'
5    def deactivate(self):
6        self.__status = 'inactive'
7    def summary(self):
8        return f'{self.username}:{self.__status}'
9
10u = User('alice')
11u.deactivate()
12print(u.summary())
22
Bug Spotting

Find the bug related to encapsulation in this code and fix it so external code cannot modify balance directly.

Click on the line(s) that contain the bug.

python
1
class Wallet:
2
    def __init__(self, owner, balance):
3
        self.owner = owner
4
        self.balance = balance
5
    def add_funds(self, amount):
6
        if amount > 0:
7
            self.balance += amount
8
 
9
w = Wallet('Sam', 50)
10
w.balance = -100  # external modification
11
 
23
Bug Spotting

Identify and fix the issue with name mangling so the subclass method works correctly.

Click on the line(s) that contain the bug.

python
1
class Config:
2
    def __init__(self):
3
        self.__secret = 'token'
4
 
5
class App(Config):
6
    def reveal(self):
7
        return self.__secret
8
 
9
app = App()
10
print(app.reveal())
11
 
24
Matching

Match each member type with how it is typically declared in Python.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match the encapsulation-related term with its description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code so that BalanceTracker encapsulates its internal amount and exposes safe operations.

python
1
class BalanceTracker:
2
def __init__(self, start_amount):
3
self. = start_amount
4
def increase(self, delta):
5
if delta > 0:
6
self. += delta
7
def current(self):
8
return self.
9

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code so the subclass can access a protected attribute while still hiding a sensitive token.

python
1
class ServiceBase:
2
def __init__(self, name, token):
3
self._service_name = name
4
self. = token
5
6
class ServiceClient(ServiceBase):
7
def label(self):
8
return self._service_name.upper()
9
def masked_token(self):
10
return '***' + self.[-2:]
11

Click an option to fill blank 1:

28
Hotspot Selection

Click the line that breaks encapsulation by directly modifying a private attribute from outside the class.

Click on the line to select.

python
1
class Account:
2
    def __init__(self, owner, balance):
3
        self.owner = owner
4
        self.__balance = balance
5
 
6
acct = Account('Riley', 500)
7
acct._Account__balance = 0
8
print(acct.owner)
29
Hotspot Selection

Click the line where a subclass incorrectly accesses a private attribute instead of using the provided method.

Click on the line to select.

python
1
class Profile:
2
    def __init__(self, username, email):
3
        self.username = username
4
        self.__email = email
5
    def get_email(self):
6
        return self.__email
7
 
8
class PublicProfile(Profile):
9
    def info(self):
10
        return self.username + ' <' + self.__email + '>'
11