Python Questions and Answers – Polymorphism

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Polymorphism”.

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
View Answer

Answer: d
Explanation: Polymorphism is a feature of object-oriented programming languages. It allows for the implementation of elegant software that is well designed and easily modified.

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
View Answer

Answer: c
Explanation: Polymorphism allows for the implementation of elegant software.

3. 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
View Answer

Answer: c
Explanation: 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.
advertisement
advertisement

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
View Answer

Answer: a
Explanation: 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.

5. What will be the output of the following Python code?

advertisement
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
View Answer

Answer: b
Explanation: The derived class method change() overrides the base class method.
advertisement

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
View Answer

Answer: a
Explanation: A class in which one or more methods are unimplemented or implemented for the methods throw an exception is called an abstract class.

7. Overriding means changing behaviour of methods of derived class methods in the base class.
a) True
b) False
View Answer

Answer: b
Explanation: Overriding means if there are two same methods present in the superclass and the subclass, the contents of the subclass method are executed.

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
View Answer

Answer: b
Explanation: When different objects are invoked, each of the individual classes return their individual values and hence it is printed.

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) 60
c) An exception is thrown
d) 30
View Answer

Answer: d
Explanation: The derived class B overrides base class A.

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
View Answer

Answer: a
Explanation: Demo().display() invokes the display() method in class Demo and Demo_Derived().display() invokes the display() method in class Demo_Derived.

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
View Answer

Answer: b
Explanation: The derived class B overrides base class A.

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) Syntax error
View Answer

Answer: b
Explanation: The method check is private so it can’t be accessed by the derived class. Execute the code in the Python shell.

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
View Answer

Answer: c
Explanation: Since 5*2==2*5, True is printed. Execute it in the Python shell to verify.

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
View Answer

Answer: d
Explanation: The derived class method two() overrides the method two() in the base class A.

15. 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
View Answer

Answer: a
Explanation: A public method in the base class can be overridden by the same named method in the subclass.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.