Top 20 Python Encapsulation Quiz Questions
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.👇
1. What is encapsulation in Python?
A. Breaking a program into functions.
B. Making all attributes and methods public.
C. Wrapping up data (attributes) and methods associated with that data into a single unit.
D. Wrapping up data into a single unit.
c
The feature of wrapping up data (attributes) and methods associated with that data into a single unit is called encapsulation in Python.
2. What is a getter method in Python?
A. To modify the private variable directly.
B. A method used to retrieve or read the value of private instance attribute.
C. A method used to set or update the value of private instance attribute.
D. A method that initializes the object.
b
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.
3. What does the following code output?
class MyClass:
def __init__(self):
self.__value = 10
obj = MyClass()
print(obj.__value)A. 10
B. AttributeError
C. NameError
D. None
b
__value is a private variable. Trying to access it directly from outside the class causes an AttributeError.
4. Which of the following is a valid use of a setter method in Python?
A. To retrieve the value of a private variable.
B. To set or update the value of a private instance variable.
C. To define a static method
D. To delete a class object
b
A setter method is used to update or set the value of a private instance variable.
5. Which decorator is used to define a getter method in Python?
A. @setter
B. @getter
C. @accessor
D. @property
d
The @property decorator turns a method into a getter for a read-only attribute.
6. What is data hiding in Python?
A. Encrypting data
B. Preventing direct access to class variables
C. Giving direct access to class variables
D. Storing data in another file
b
Data hiding is a mechanism to hide the data of a class from the other classes or outside the world.
7. What will this code print?
class Test:
def __init__(self):
self.__x = 5
def get_x(self):
return self.__x
t = Test()
print(t.get_x())A. 0
B. 5
C. AttributeError
D. NameError
b
The private variable __x is accessed through the getter method get_x(), which returns its value.
8. What will this code output?
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())A. 5000
B. 6000
C. AttributeError
D. NameError
b
The setter method set_salary updates the private variable __salary to 6000, which is then retrieved using the getter.
9. Why is it not recommended to access private variables directly in Python?
A. It causes the program to crash.
B. It breaks the concept of abstraction and encapsulation.
C. It makes code shorter.
D. None of the above
b
Direct access to private variables breaks the principle of encapsulation, leading to code that’s harder to maintain and less secure.
10. Which of the following is another name for a getter method in Python?
A. Constructor
B. Destructor
C. Mutator method
D. Accessor method
d
A getter method is also known as an accessor method, since it accesses and returns the value of a private attribute. A setter method is also known as mutator method because it mutates or changes the value of a private variable.
11. Which of the following represents a correct way to define and use a getter method?
class Demo:
def __init__(self):
self.__value = 42
def get_value(self):
return self.__value
d = Demo()
print( ? )A. print(d.get_value())
B. print(d.getvalue())
C. print(get_value(d))
D. print(d.__value)
a
The correct method call to retrieve the private attribute is d.get_value(). Direct access to __value will raise an error due to name mangling.
12. Which decorator is used to define a setter method when using the @property syntax in Python?
A. @setter
B. @mutator
C. @update
D. @<property-name>.setter
d
In Python, when you use the @property decorator for defining getter methods, the corresponding setter is defined using @.setter.
13. Consider the code below. Which line correctly sets the value of the private variable using a setter?
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()
A. acc.__balance = 1000
B. acc.set_balance(1000)
C. acc.balance = 1000
D. acc.balance(1000)
c
Since balance is defined using the @property and @balance.setter decorators, the correct way to set it is through assignment: acc.balance = 1000.
14. Consider the below Python code. What will be the expected output when no errors?
class BankAccount:
def __init__(self):
self.__balance = 100
@property
def balance(self):
return self.__balance + 10
account = BankAccount()
account.balance = 50
print(account.balance)A. 100
B. 60
C. 50
D. AttributeError
d
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.
15. What will be the expected output of the following code?
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)A. 100
B. 110
C. 50
D. 60
c
Always use @.setter to define setters for properties. Without the proper decorator, a method named balance will not act as a setter for the balance property.
16. Identify the correct output of the following Python code.
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)A. 100
B. 110
C. 60
D. 50
c
The @balance.setter properly defines the setter without overwriting the property. Now, account.balance = 50 calls the setter, and print(account.balance) calls the getter.
17. What does this code print?
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())A. 10 10
B. 10 20
C. 20 20
D. AttributeError
a
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.
18. Which snippet of code truly enforces immutability of id after initialization?
A.
class Employee:
def __init__(self, id):
self.__id = id
@property
def id(self):
return self.__idB.
class Employee:
def __init__(self, id):
self.id = idC.
class Employee:
def __init__(self, id):
self.__id = id
def set_id(self, new_id):
self.__id = new_idD.
class Employee:
def __init__(self, id):
self.__id = id
@id.setter
def id(self, value):
raise AttributeError("ID is immutable!")a
Option A provides a getter without a setter, making __id read-only.
19. What is the flaw in this “encapsulation”?
class 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)A. No flaw — it correctly converts Celsius to Fahrenheit.
B. The getter should use __temp for true privacy.
C. _temp is accessible externally, violating encapsulation.
D. The class lacks a setter method.
c
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.
20. What happens when you run this?
class Test:
__slots__ = ['__x']
def __init__(self):
self.__x = 10
t = Test()
print(t.__x)A. 10
B. AttributeError
C. TypeError
D. _Test__x is printed.
b
__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.
Quiz Results
0
Total Questions
0
Correct Answers
0
Incorrect Answers
0%
Score





