Welcome to this challenging Python encapsulation quiz! In this quiz, we have compiled the top 20 multiple-choice questions based on Python encapsulation.
These 20 MCQ quizzes will test your knowledge from basic to advanced levels, as well as level up your understanding and knowledge in encapsulation. Topics covered private variables, getter and setter methods, data hiding, and property decorators in Python.
Whether you’re a beginner or an experienced Python programmer, this quiz is perfect for interviews preparation, competitive exams, and enhancing your knowledge in encapsulation.
So, are you ready to score 20/20? Let’s see, how well do you really understand encapsulation concept in Python? Let’s begin with question number 1.👇
- A method which is used to retrieve / get / read the value of private instance attribute (i.e.
- instance variable) is called getter method in Python.
- This method is also known as accessor method in Python.
class MyClass:
def __init__(self):
self.__value = 10
obj = MyClass()
print(obj.__value)class Test:
def __init__(self):
self.__x = 5
def get_x(self):
return self.__x
t = Test()
print(t.get_x())class Employee:
def __init__(self, salary):
self.__salary = salary
def set_salary(self, value):
self.__salary = value
def get_salary(self):
return self.__salary
e = Employee(5000)
e.set_salary(6000)
print(e.get_salary())class Demo:
def __init__(self):
self.__value = 42
def get_value(self):
return self.__value
d = Demo()
print( ? )class Account:
def __init__(self):
self.__balance = 0
@property
def balance(self):
return self.__balance
@balance.setter
def balance(self, amount):
self.__balance = amount
acc = Account()
class BankAccount:
def __init__(self):
self.__balance = 100
@property
def balance(self):
return self.__balance + 10
account = BankAccount()
account.balance = 50
print(account.balance)- Since the getter method is defined with the @property decorator that adds 10 to __balance.
- Therefore, the __balance = 110.
- But the setter method is not defined using @property.balance.
- Therefore, the line account.balance = 50 raises AttributeError at runtime.
class BankAccount:
def __init__(self):
self.__balance = 100
@property
def balance(self):
return self.__balance + 10
def balance(self, amount):
self.__balance = amount
account = BankAccount()
account.balance = 50
print(account.balance)class BankAccount:
def __init__(self):
self.__balance = 100
@property
def balance(self):
return self.__balance + 10
@balance.setter
def balance(self, amount):
self.__balance = amount
account = BankAccount()
account.balance = 50
print(account.balance)class Test:
def __init__(self):
self.__x = 10
def get_x(self):
return self.__x
t1 = Test()
t2 = Test()
t2.__x = 20
print(t1.get_x(), t2.get_x())- In Python, when you define a variable with double underscores (like __x), it gets name mangled.
- That means self.__x becomes internally: self._Test__x.
- The line t2.__x = 20 does not modify the existing private variable __x (i.e., _Test__x) inside the object t2.
- Therefore, it creates a new attribute named __x that is not private and not name-mangled.
- So, t2.__x exists, but it doesn’t modify the original private variable.
- The method get_x() is returning self.__x, which actually means self._Test__x.
- Since self._Test__x was initialized to 10 and never changed, both t1.get_x() and t2.get_x() return 10.
class Employee:
def __init__(self, id):
self.__id = id
@property
def id(self):
return self.__id
class Employee:
def __init__(self, id):
self.id = id
class Employee:
def __init__(self, id):
self.__id = id
def set_id(self, new_id):
self.__id = new_id
class Employee:
def __init__(self, id):
self.__id = id
@id.setter
def id(self, value):
raise AttributeError("ID is immutable!")
class Employee:
def __init__(self, id):
self.__id = id
@property
def id(self):
return self.__idclass Temperature:
def __init__(self):
self._temp = 0
@property
def temp(self):
return self._temp * 1.8 + 32
t = Temperature()
t._temp = 100
print(t.temp)- The variable _temp is protected by convention (using a single underscore), but Python does not enforce access restrictions.
- Since the protected variable _temp can still be directly accessed and modified from outside the class, it violates the principle of encapsulation, which aims to hide internal state.
- A better encapsulation approach would be make the variable private self.__temp and provide a setter method for controlled modification.
- However, options C and D have partial truth, B directly points out the core flaw in terms of encapsulation.
class Test:
__slots__ = ['__x']
def __init__(self):
self.__x = 10
t = Test()
print(t.__x)- __slots__ restricts attribute creation.
- If an attribute is not in the __slots__ list, Python will raise an AttributeError when you try to assign it.
- Python automatically mangles variables starting with double underscores (__).
- So, self.__x becomes internally self._Test__x due to name mangling.
- But you wrote ‘__x’ in __slots__, not ‘_Test__x’, which is not listed in __slots__.
- This mismatch causes Python to raise an AttributeError.




