This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Encapsulation”.
1. Which of these is not a fundamental features of OOP?
a) Encapsulation
b) Inheritance
c) Instantiation
d) Polymorphism
View Answer
Explanation: Encapsulation, Inheritance, and Polymorphism are fundamental features of Object-Oriented Programming (OOP). Instantiation is the process of creating an object from a class, but it is not considered a fundamental OOP concept.
2. Which of the following is the most suitable definition for encapsulation?
a) Ability of a class to derive members of another class as a part of its own definition
b) Means of bundling instance variables and methods in order to restrict access to certain class members
c) Focuses on variables and passing of variables to functions
d) Allows for implementation of elegant software that is well designed and easily modified
View Answer
Explanation: Encapsulation is the concept of wrapping data (variables) and code (methods) together while restricting direct access to some of the object’s components to protect the integrity of the data.
3. What will be the output of the following Python code?
class Demo: def __init__(self): self.a = 1 self.__b = 1 def display(self): return self.__b obj = Demo() print(obj.a)
a) The program has an error because there isn’t any function to return self.a
b) The program has an error because b is private and display(self) is returning a private member
c) The program runs fine and 1 is printed
d) The program has an error as you can’t name a class member using __b
View Answer
Explanation: The attribute a is public and can be accessed directly with obj.a, so 1 is printed. The private variable __b is accessed only inside the class and doesn’t affect accessing a.
4. What will be the output of the following Python code?
class Demo: def __init__(self): self.a = 1 self.__b = 1 def display(self): return self.__b obj = Demo() print(obj.__b)
a) The program has an error because there isn’t any function to return self.a
b) The program has an error because b is private and display(self) is returning a private member
c) The program has an error because b is private and hence can’t be printed
d) The program runs fine and 1 is printed
View Answer
Explanation: The code throws an error because __b is a private variable and cannot be accessed directly outside the class. Attempting to print obj.__b raises an AttributeError due to name mangling. Accessing it through a method like display() would work, but direct access is not allowed.
5. Methods of a class that provide access to private members of the class are called as ______ and ______
a) getters/setters
b) __repr__/__str__
c) user-defined functions/in-built functions
d) __init__/__del__
View Answer
Explanation: Getters and setters are special methods used to access (get) and update (set) private variables of a class. They provide a controlled way to read or modify private data, ensuring data encapsulation and validation when needed.
6. Which of these is a private data field?
def Demo: def __init__(self): __a = 1 self.__b = 1 self.__c__ = 1 __d__= 1
a) __a
b) __b
c) __c__
d) __d__
View Answer
Explanation: self.__b is a private instance variable due to name mangling in Python (it becomes _Demo__b).
- __a and __d__ are local variables inside the constructor, not class fields.
- __c__ uses double underscores before and after, which by convention is reserved for special methods or attributes, not private fields.
7. What will be the output of the following Python code?
class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__b obj = Demo() print(obj.get())
a) The program has an error because there isn’t any function to return self.a
b) The program has an error because b is private and display(self) is returning a private member
c) The program has an error because b is private and hence can’t be printed
d) The program runs fine and 1 is printed
View Answer
Explanation: The private variable __b is accessed within the class using the get() method. Since get() returns self.__b, calling obj.get() correctly returns 1. Accessing private variables from within class methods is allowed, so the program runs without any error and prints 1.
8. What will be the output of the following Python code?
class Demo: def __init__(self): self.a = 1 self.__b = 1 def get(self): return self.__b obj = Demo() obj.a=45 print(obj.a)
a) The program runs properly and prints 45
b) The program has an error because the value of members of a class can’t be changed from outside the class
c) The program runs properly and prints 1
d) The program has an error because the value of members outside a class can only be changed as self.a=45
View Answer
Explanation: The public variable a can be changed directly from outside the class. Setting obj.a = 45 updates its value, so print(obj.a) outputs 45 without any error.
9. Private members of a class cannot be accessed.
a) True
b) False
View Answer
Explanation: Private members (with double underscores) cannot be accessed directly outside the class, but they can still be accessed indirectly through name mangling or via public methods like getters and setters.
10. The purpose of name mangling is to avoid unintentional access of private class members.
a) True
b) False
View Answer
Explanation: Name mangling in Python changes the name of private variables (those with double underscores) internally to prevent accidental access or modification from outside the class, helping to protect private members.
11. What will be the output of the following Python code?
class fruits: def __init__(self): self.price = 100 self.__bags = 5 def display(self): print(self.__bags) obj=fruits() obj.display()
a) The program has an error because display() is trying to print a private class member
b) The program runs fine but nothing is printed
c) The program runs fine and 5 is printed
d) The program has an error because display() can’t be accessed
View Answer
Explanation: The private variable __bags is accessible inside the class methods, so display() can print its value 5 without any error.
12. What will be the output of the following Python code?
class student: def __init__(self): self.marks = 97 self.__cgpa = 8.7 def display(self): print(self.marks) obj=student() print(obj._student__cgpa)
a) The program runs fine and 8.7 is printed
b) Error because private class members can’t be accessed
c) Error because the proper syntax for name mangling hasn’t been implemented
d) The program runs fine but nothing is printed
View Answer
Explanation: The private variable __cgpa is name-mangled by Python to _student__cgpa. Accessing it directly using obj._student__cgpa works and prints 8.7.
13. Which of the following is false about protected class members?
a) They begin with one underscore
b) They can be accessed by subclasses
c) They can be accessed by name mangling method
d) They can be accessed within a class
View Answer
Explanation: Protected members (single underscore prefix) are a convention indicating they should be treated as non-public but are still accessible directly and by subclasses. Name mangling is only applied to private members (double underscore prefix), not protected members.
14. What will be the output of the following Python code?
class objects: def __init__(self): self.colour = None self._shape = "Circle" def display(self, s): self._shape = s obj=objects() print(obj._objects_shape)
a) The program runs fine because name mangling has been properly implemented
b) Error because the member shape is a protected member
c) Error because the proper syntax for name mangling hasn’t been implemented
d) Error because the member shape is a private member
View Answer
Explanation: The attribute _shape is a protected member (single underscore), not private, so name mangling does not apply. Accessing obj._objects_shape causes an AttributeError because the name is incorrect — the correct attribute is obj._shape.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Python Books
- Apply for Programming Internship
- Apply for Python Internship
- Practice Programming MCQs
- Check Information Technology Books