Python Multiple Choice Questions & Answers (MCQs) focuses on “Polymorphism”.
Q 1. 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
Show Answer
Answer:-D. Allows for objects of different types and behaviour to be treated as the same general typeExplanation
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 2. 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
Show Answer
Answer:-C. The program will have a more elegant design and will be easier to maintain and updateExplanation
Polymorphism allows for the implementation of elegant software.
Q 3. What is the use of duck typing?
A. More restriction on the type values that can be passed to a given method
B. Less restriction on the type values that can be passed to a given method
C. No restriction on the type values that can be passed to a given method
D. Makes the program code smaller
Show Answer
Answer:-B. Less restriction on the type values that can be passed to a given methodExplanation
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 4. What will be the output of the following Python code?
class A:
def __str__(self):
return ‘1’
class B(A):
def __init__(self):
super().__init__()
class C(B):
def __init__(self):
super().__init__()
def main():
obj1 = B()
obj2 = A()
obj3 = C()
print(obj1, obj2,obj3)
main()
A. 1 1 1
B. 1 2 3
C. ‘1’ ‘1’ ‘1’
D. An exception is thrown
Show Answer
Answer:-A. 1 1 1Explanation
The super().__init__() in the subclasses has been properly invoked and none of other subclasses return any other value. Hence 1 is returned each time the object is created and printed.Q 5. 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
Show Answer
Answer:-B. 2Explanation
The derived class method change() overrides the base class method.
Q 6. A class in which one or more methods are only implemented to raise an exception is called an abstract class.
A. True
B. False
Show Answer
Answer:-A. TrueExplanation
A class in which one or more methods are unimplemented or implemented for the methods throw an exception is called an abstract class.Q 7. Overriding means changing behaviour of methods of derived class methods in the base class.
A. True
B. False
Show Answer
Answer:-B. FalseExplanation
Overriding means if there are two same methods present in the superclass and the subclass, the contents of the subclass method are executed.Q 8. 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
Show Answer
Answer:-D. An exception is thrownExplanation
In the print() statement, the variables obj1, obj2, obj3 are used instead of o1, o2, o3. Since obj1, obj2, and obj3 were never defined anywhere in the program, Python will raise a NameError, stating that these variables are not found.
Q 9. 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. 15
B. 30
C. 60
D. An exception is thrown
Show Answer
Answer:-B. 30Explanation
The derived class B overrides base class A.Q 10. What will be the output of the following Python code?
class Demo:
def check(self):
return ” Demo’s check “
def display(self):
print(self.check())
class Demo_Derived(Demo):
def check(self):
return ” Derived’s check “
Demo().display()
Demo_Derived().display()
A. Demo’s check Derived’s check
B. Demo’s check Demo’s check
C. Derived’s check Demo’s check
D. Syntax error
Show Answer
Answer:-A. Demo’s check Derived’s checkExplanation
Demo().display() invokes the display() method in class Demo and Demo_Derived().display() invokes the display() method in class Demo_Derived.Q 11. 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. 15
B. 30
C. An exception is thrown
D. 60
Show Answer
Answer:-A. 15 ,B. 30Explanation
The derived class B overrides base class A.
Q 12. What will be the output of the following Python code?
class Demo:
def __check(self):
return ” Demo’s check “
def display(self):
print(self.check())
class Demo_Derived(Demo):
def __check(self):
return ” Derived’s check “
Demo().display()
Demo_Derived().display()
A. Demo’s check Derived’s check
B. Demo’s check Demo’s check
C. Derived’s check Demo’s check
D. Error
Show Answer
Answer:-B. Demo’s check Demo’s checkExplanation
The method __check() is private due to the double underscore (__), making it inaccessible outside the class. The display() method incorrectly tries to call self.check(), which does not exist, leading to an AttributeError at runtime.Q 13. 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. 1
C. True
D. An exception is thrown
Show Answer
Answer:-C. TrueExplanation
Since 5*2==2*5, True is printed. Execute it in the Python shell to verify.Q 14. What will be the output of the following Python code?
class A:
def one(self):
return self.two()
def two(self):
return ‘A’
class B(A):
def two(self):
return ‘B’
obj2=B()
print(obj2.two())
A. A
B. An exception is thrown
C. A B
D. B
Show Answer
Answer:-D. BExplanation
The derived class method two() overrides the method two() in the base class A.
Q 15. Which of the following statements is true?
A. Overriding isn’t possible in Python
B. A subclass method can be overridden by the superclass
C. A private method in a superclass can be overridden
D. A non-private method in a superclass can be overridden


Leave a Reply