Test Series - python

Test Number 109/108

Q: What will be the output of the following Python code?

class A:
    def __init__(self):
        self.multiply(15)
    def multiply(self, i):
        self.i = 4 * i;
class B(A):
    def __init__(self):
        super().__init__()
        print(self.i)
 
    def multiply(self, i):
        self.i = 2 * i;
obj = B()
A. An exception is thrown
B. 15
C. 30
D. 60
Solution: The derived class B overrides base class A.
Q: What will be the output of the following Python code?

class A:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __str__(self):
        return 1
    def __eq__(self, other):
        return self.x * self.y == other.x * other.y
obj1 = A(5, 2)
obj2 = A(2, 5)
print(obj1 == obj2)
A. False
B. True
C. 0
D. 1
Solution: Since 5*2==2*5, True is printed. Execute it in the Python shell to verify.
Q: Which of the following statements is true?
A. A non-private method in a superclass can be overridden
B. A subclass method can be overridden by the superclass
C.  A private method in a superclass can be overridden
D. Overriding isn’t possible in Python
Solution: A public method in the base class can be overridden by the same named method in the subclass.
Q: Which of these is not a fundamental features of OOP?
A. Encapsulation
B. Inheritance
C. Instantiation
D. Polymorphism
Solution: Instantiation simply refers to creation of an instance of class. It is not a fundamental feature of OOP.
Q: 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
Solution: The values assigned by the constructor to the class members is used to create the object.
Q: 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
Solution: The program has no error because the class member which is public is printed. 1 is displayed. Execute in python shell to verify.
Q: 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
Solution: Variables beginning with two underscores are said to be private members of the class and they can’t be accessed directly.

You Have Score    /7