Python Multiple Choice Questions on “Encapsulation”.
1. Which of these is not a fundamental features of OOP?
a) Encapsulation
b) Inheritance
c) Instantiation
d) Polymorphism
Answer: c
Clarification: Instantiation simply refers to creation of an instance of class. It is not a fundamental feature of OOP.
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) on variables and passing of variables to functions
d) Allows for implementation of elegant software that is well designed and easily modified
Answer: b
Clarification: The values assigned by the constructor to the class members is used to create the object.
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
Answer: c
Clarification: The program has no error because the class member which is public is printed. 1 is displayed. Execute in python shell to verify.
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
Answer: c
Clarification: Variables beginning with two underscores are said to be private members of the class and they can’t be accessed directly.
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__
Answer: a
Clarification: The purpose of getters and setters is to get(return) and set(assign) private instance variables of a class.
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__
Answer: b
Clarification: Variables such as self.__b are private members of the class.
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
Answer: d
Clarification: Here, get(self) is a member of the class. Hence, it can even return a private member of the class. Because of this reason, the program runs fine and 1 is printed.
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
