Last Updated: January 3, 2026
29 quizzes
Access the current balance of the account using the provided method instead of touching internal state directly.
current = account.()Click an option to fill the blank:
Store a new salary value using the public method instead of assigning directly to the internal attribute.
employee.(75000)Click an option to fill the blank:
Read a mangled attribute of the Logger class by using its internal transformed name.
print(logger._Loggersecret_token.)Click an option to fill blank 1 of 2:
What does encapsulation mainly help you achieve in a Python class?
In Python, what is the default access level of a class attribute without underscores?
Which attribute name most clearly signals a protected attribute by convention?
Why might you mark an attribute as private using double underscores in a base class?
In a banking app, which design best respects encapsulation for updating an account balance?
What is the internal name of attribute __token defined in class Session?
How should external code typically access a private attribute that stores configuration?
Which statement about protected members is most accurate in Python?
In a subclass, how do you normally access a protected attribute _config defined in the base class?
Which option best describes a public method in an encapsulated class?
If both Parent and Child define __value attributes, what does name mangling ensure?
Order the steps to design an encapsulated BankAccount class and use it to deposit money.
Drag and drop to reorder, or use the arrows.
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.
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())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())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)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'))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())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.
class Wallet: def __init__(self, owner, balance): self.owner = owner self.balance = balance def add_funds(self, amount): if amount > 0: self.balance += amount w = Wallet('Sam', 50)w.balance = -100 # external modification Identify and fix the issue with name mangling so the subclass method works correctly.
Click on the line(s) that contain the bug.
class Config: def __init__(self): self.__secret = 'token' class App(Config): def reveal(self): return self.__secret app = App()print(app.reveal()) 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.
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.
Complete the code so that BalanceTracker encapsulates its internal amount and exposes safe operations.
class BalanceTracker: def __init__(self, start_amount): self. = start_amount def increase(self, delta): if delta > 0: self. += delta def current(self): return self.Click an option to fill blank 1:
Complete the code so the subclass can access a protected attribute while still hiding a sensitive token.
class ServiceBase: def __init__(self, name, token): self._service_name = name self. = tokenclass ServiceClient(ServiceBase): def label(self): return self._service_name.upper() def masked_token(self): return '***' + self.[-2:]Click an option to fill blank 1:
Click the line that breaks encapsulation by directly modifying a private attribute from outside the class.
Click on the line to select.
class Account: def __init__(self, owner, balance): self.owner = owner self.__balance = balance acct = Account('Riley', 500)acct._Account__balance = 0print(acct.owner)Click the line where a subclass incorrectly accesses a private attribute instead of using the provided method.
Click on the line to select.
class Profile: def __init__(self, username, email): self.username = username self.__email = email def get_email(self): return self.__email class PublicProfile(Profile): def info(self): return self.username + ' <' + self.__email + '>'