Test Series - python

Test Number 108/108

Q: Which of the following best describes polymorphism?
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 objects of different types and behaviour to be treated as the same general type
Solution: Polymorphism is a feature of object-oriented programming languages. It allows for the implementation of elegant software that is well designed and easily modified.
Q: What is the biggest reason for the use of polymorphism?
A. It allows the programmer to think at a more abstract level
B. There is less program code to write
C. The program will have a more elegant design and will be easier to maintain and update
D. Program code takes up less space
Solution: Polymorphism allows for the implementation of elegant software.
Q: What is the use of duck typing?
A. More restriction on the type values that can be passed to a given method
B. No restriction on the type values that can be passed to a given method
C. Less restriction on the type values that can be passed to a given method
D. Makes the program code smaller
Solution: In Python, any set of classes with a common set of methods can be treated similarly. This is called duck typing. Hence duck typing imposes less restrictions.
Q: What will be the output of the following Python code?

class Demo:
    def __init__(self):
        self.x = 1
    def change(self):
        self.x = 10
class Demo_derived(Demo):
    def change(self):
        self.x=self.x+1
        return self.x
def main():
    obj = Demo_derived()
    print(obj.change())
 
main()
A. 11
B. 2
C. 1
D. An exception is thrown
Solution: The derived class method change() overrides the base class method.
Q: A class in which one or more methods are only implemented to raise an exception is called an abstract class.
A. True
B. False
C. none
D. ..
Solution: A class in which one or more methods are unimplemented or implemented for the methods throw an exception is called an abstract class.
Q: Overriding means changing behaviour of methods of derived class methods in the base class.
A. True
B. False
C. none
D. ,,
Solution: Overriding means if there are two same methods present in the superclass and the subclass, the contents of the subclass method are executed.
Q: What will be the output of the following Python code?

class A:
    def __repr__(self):
        return "1"
class B(A):
    def __repr__(self):
        return "2"
class C(B):
    def __repr__(self):
        return "3"
o1 = A()
o2 = B()
o3 = C()
print(obj1, obj2, obj3)
A. 1 1 1
B. 1 2 3
C. ‘1’ ‘1’ ‘1’
D. An exception is thrown
Solution: When different objects are invoked, each of the individual classes return their individual values and hence it is printed.
Q: What will be the output of the following Python code?

class A:
    def __init__(self):
        self.multiply(15)
        print(self.i)
 
    def multiply(self, i):
        self.i = 4 * i;
class B(A):
    def __init__(self):
        super().__init__()
 
    def multiply(self, i):
        self.i = 2 * i;
obj = B()
A. An exception is thrown
B. 15
C. 60
D. 30
Solution: The derived class B overrides base class A.

You Have Score    /8