Welcome to the Python Inheritance and Polymorphism MCQs! Inheritance is a key idea in object-oriented programming that permits a brand new class to inherit attributes and strategies from an current class. Polymorphism, alternatively, refers back to the skill of a technique to behave in a different way primarily based on the item it’s known as on. These questions will take a look at your understanding of those necessary ideas in Python, together with how inheritance works, the right way to create subclasses, and the way polymorphism enhances code flexibility. Every query is multiple-choice, with just one right reply. Take your time to rigorously learn every query and select the most suitable choice. Let’s discover Python inheritance and polymorphism collectively!

30+ MCQs on Python Inheritance and Polymorphism
Q1. What’s inheritance in Python?
a) It permits a category to inherit attributes and strategies from one other class
b) It permits a category to create a number of cases
c) It permits a category to override its mum or dad class strategies
d) It permits a category to cover its attributes and strategies
Reply: a
Clarification: Inheritance in Python permits a category (subclass) to inherit attributes and strategies from one other class (superclass).
Q2. Which key phrase is used to inherit a category in Python?
a) tremendous
b) base
c) derive
d) extends
Reply: d
Clarification: The extends
key phrase is used to point {that a} class is inheriting from one other class in Python.
Q3. What’s the idea of polymorphism in Python?
a) It permits a category to have a number of constructors
b) It permits a category to have a number of strategies with the identical title however totally different parameters
c) It permits a category to have a number of cases
d) It permits a category to have a number of inheritance
Reply: b
Clarification: Polymorphism in Python permits a category to have a number of strategies with the identical title however totally different parameters, which could be invoked primarily based on the enter arguments.
This autumn. What’s methodology overriding in Python?
a) It permits a subclass to have its personal methodology with the identical title as within the superclass
b) It permits a subclass to inherit all strategies from the superclass
c) It permits a superclass to have strategies with totally different names
d) It permits a subclass to have its personal constructor
Reply: a
Clarification: Methodology overriding is the power of a subclass to offer a selected implementation of a technique that’s already outlined in its superclass.
Q5. What’s the goal of the init() methodology in Python lessons?
a) The init() methodology is used to initialize the item’s state.
b) The init() methodology is used to outline class variables.
c) The init() methodology is used to outline occasion strategies.
d) The init() methodology is used to name one other methodology.
Reply: a
Clarification: The init() methodology in Python is a particular methodology that’s robotically known as when a brand new object of a category is instantiated. It’s used to initialize the item’s state.
Q6. Contemplate the next code:
class Animal:
def sound(self):
print("Animal sound")
class Canine(Animal):
def sound(self):
print("Bark")
class Cat(Animal):
def sound(self):
print("Meow")
a = Animal()
d = Canine()
c = Cat()
a.sound()
d.sound()
c.sound()
What would be the output?
a) Animal sound Bark Meow
b) Bark Meow Animal sound
c) Animal sound Animal sound Animal sound
d) Bark Bark Bark
Reply: a
Clarification: Every object calls its respective sound
methodology. a.sound()
calls the sound
methodology of the Animal
class, d.sound()
calls the sound
methodology of the Canine
class, and c.sound()
calls the sound
methodology of the Cat
class.
Q7. What’s the output of the next code?
class Form:
def space(self):
cross
class Sq.(Form):
def __init__(self, facet):
self.facet = facet
def space(self):
return self.facet * self.facet
class Circle(Form):
def __init__(self, radius):
self.radius = radius
def space(self):
return 3.14 * self.radius * self.radius
s = Sq.(5)
c = Circle(2)
print("Space of Sq.:", s.space())
print("Space of Circle:", c.space())
a) Space of Sq.: 25 Space of Circle: 12.56
b) Space of Sq.: 10 Space of Circle: 6.28
c) Space of Sq.: 20 Space of Circle: 10
d) Space of Sq.: 12.56 Space of Circle: 25
Reply: a
Clarification: The Sq.
class has an space
methodology that calculates the world of a sq., and the Circle
class has an space
methodology that calculates the world of a circle. When objects s
and c
are created and their space
strategies are known as, the output will probably be Space of Sq.: 25 and Space of Circle: 12.56.
Q8. What’s the output of the next code?
class A:
def present(self):
print("A")
class B(A):
def present(self):
print("B")
class C(A):
def present(self):
print("C")
class D(B, C):
cross
obj = D()
obj.present()
a) A
b) B
c) C
d) Not one of the above
Reply: b
Clarification: Since D
doesn’t have its personal present
methodology, it inherits from B
which is the primary superclass listed within the class definition. Subsequently, obj.present()
will print “B”.
Q9. What’s the output of the next code?
class A:
def present(self):
print("A")
class B(A):
cross
class C(A):
def present(self):
print("C")
class D(B, C):
cross
obj = D()
obj.present()
a) A
b) C
c) Error: Can not decide which present methodology to name
d) Not one of the above
Reply: b
Clarification: The category D
inherits from B
and C
. Since C
is the primary superclass listed in D
, the present
methodology of C
is invoked. Subsequently, obj.present()
will print “C”.
Q10. What’s the output of the next code?
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
class C(A):
def __init__(self):
print("C")
class D(B, C):
cross
obj = D()
a) A
b) B
c) C
d) Not one of the above
Reply: b
Clarification: When an object of sophistication D
is created, the __init__
of the primary superclass within the MRO is named, which is B
. Subsequently, Init B
will probably be printed.
Q11. What’s the output of the next code?
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
tremendous().__init__()
class C(A):
def __init__(self):
print("C")
tremendous().__init__()
class D(B, C):
def __init__(self):
print("D")
tremendous().__init__()
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Reply: b
Clarification: When an object of sophistication D
is created, D
‘s __init__
methodology explicitly calls B
‘s __init__
methodology, which in flip calls A
‘s __init__
methodology. Then, D
‘s __init__
methodology additionally explicitly calls C
‘s __init__
methodology, which calls A
‘s __init__
methodology once more. So the order of output is D, B, C, A.
Q12. What’s the output of the next code?
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
tremendous().__init__()
class C(A):
def __init__(self):
print("C")
tremendous().__init__()
class D(B, C):
cross
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Reply: b
Clarification: When an object of sophistication D
is created, D
‘s __init__
methodology explicitly calls B
‘s __init__
methodology, which in flip calls A
‘s __init__
methodology. Then, D
‘s __init__
methodology additionally explicitly calls C
‘s __init__
methodology, which calls A
‘s __init__
methodology once more. So the order of output is D, B, C, A.
Q13. What’s the output of the next code?
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
tremendous().__init__()
class C(A):
def __init__(self):
print("C")
tremendous().__init__()
class D(B, C):
def __init__(self):
print("D")
B.__init__(self)
C.__init__(self)
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Reply: b
Clarification: On this case, D
‘s __init__
methodology explicitly calls B
‘s __init__
methodology, which in flip calls A
‘s __init__
methodology. Then, D
‘s __init__
methodology additionally explicitly calls C
‘s __init__
methodology, which calls A
‘s __init__
methodology once more. So the order of output is D, B, C, A.
Q14. What’s the output of the next code?
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
A.__init__(self)
class C(A):
def __init__(self):
print("C")
A.__init__(self)
class D(B, C):
def __init__(self):
print("D")
B.__init__(self)
C.__init__(self)
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Reply: b
Clarification: On this case, D
‘s __init__
methodology explicitly calls B
‘s __init__
methodology, which calls A
‘s __init__
methodology. Then, D
‘s __init__
methodology additionally explicitly calls C
‘s __init__
methodology, which calls A
‘s __init__
methodology once more. So the order of output is D, B, C, A.
Q15. What’s the goal of the “cross” assertion in Python?
a) The “cross” assertion is used to point that the tactic will not be carried out and can do nothing.
b) The “cross” assertion is used to exit a loop.
c) The “cross” assertion is used to outline a category.
d) The “cross” assertion is used to name one other methodology.
Reply: a
Clarification: The “cross” assertion in Python is used as a placeholder when an announcement is required syntactically however you don’t want any command or code to execute.
Q16. What’s the output of the next code?
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
tremendous().__init__()
class C(A):
def __init__(self):
print("C")
tremendous().__init__()
class D(B, C):
cross
obj = D()
a) A B C D
b) D B C A
c) D B A
d) D C A
Reply: b
Clarification: When an object of sophistication D
is created, D
‘s __init__
methodology calls B
‘s __init__
methodology, which in flip calls A
‘s __init__
methodology. Then, D
‘s __init__
methodology calls C
‘s __init__
methodology, which additionally calls A
‘s __init__
methodology. So the order of output is D, B, C, A.
Q17. What’s an summary class in Python?
a) An summary class is a category that can’t be instantiated and is used as a blueprint for different lessons.
b) An summary class is a category that’s instantiated a number of occasions.
c) An summary class is a category with solely static strategies.
d) An summary class is a category with solely personal strategies.
Reply: a
Clarification: An summary class in Python is a category that can’t be instantiated and is supposed for use as a base class for different lessons. It could comprise summary strategies that should be carried out by its subclasses.
Q18. What’s the output of the next code?
class A:
def __init__(self):
print("A")
class B(A):
def __init__(self):
print("B")
tremendous().__init__()
class C(A):
def __init__(self):
print("C")
tremendous().__init__()
class D(B, C):
cross
class E(D):
cross
obj = E()
a) A B C D
b) D B C A
c) D B A
d) D C A
Reply: b
Clarification: When an object of sophistication E
is created, E
‘s __init__
methodology calls D
‘s __init__
methodology, which in flip calls B
‘s __init__
methodology, then C
‘s __init__
methodology, and at last A
‘s __init__
methodology. So the order of output is D, B, C, A.
Q19. How are you going to create an object of an summary class in Python?
a) Through the use of the “tremendous()” perform
b) Through the use of the “new” key phrase
c) Through the use of the “staticmethod” decorator
d) By making a subclass and implementing the summary strategies
Reply: d
Clarification: To create an object of an summary class in Python, you might want to create a subclass that inherits from the summary class and implement all of the summary strategies outlined within the summary class.
Q20. What’s the output of the next code?
class A:
def present(self):
print("A")
class B(A):
def present(self):
print("B")
class C(A):
def present(self):
print("C")
class D(B, C):
cross
obj = D()
obj.present()
a) A
b) B
c) C
d) Not one of the above
Reply: b
Clarification: Since D
doesn’t have its personal present
methodology, it inherits from B
which is the primary superclass listed within the class definition. Subsequently, obj.present()
will print “B”.
Q21. What’s the output of the next code?
class A:
def present(self):
print("A")
class B(A):
cross
class C(A):
def present(self):
print("C")
class D(B, C):
cross
obj = D()
obj.present()
a) A
b) C
c) Error: Can not decide which present methodology to name
d) Not one of the above
Reply: b
Clarification: The category D
inherits from B
and C
. Since C
is the primary superclass listed in D
, the present
methodology of C
is invoked. Subsequently, obj.present()
will print “C”.
Q22. What’s the fundamental benefit of utilizing inheritance in Python?
a) It permits for code reusability and reduces redundancy.
b) It makes the code extra advanced and tougher to take care of.
c) It enforces encapsulation of information.
d) It prevents the creation of latest lessons.
Reply: a
Clarification: Inheritance promotes code reusability by permitting new lessons to make use of current code from mum or dad lessons.
Q23. What’s the goal of polymorphism in Python?
a) Polymorphism permits strategies to be carried out in several methods in youngster lessons.
b) Polymorphism is used to make all strategies personal.
c) Polymorphism is a option to forestall inheritance.
d) Polymorphism is used to limit entry to attributes.
Reply: a
Clarification: Polymorphism permits strategies in several lessons to have the identical title however behave in a different way.
Q24. What’s the distinction between methodology overloading and methodology overriding in Python?
a) Methodology overloading permits a category to have a number of strategies with the identical title however totally different signatures, whereas methodology overriding permits a baby class to offer a selected implementation of a technique that’s already outlined in its mum or dad class.
b) Methodology overloading will not be supported in Python, solely methodology overriding.
c) Methodology overloading and methodology overriding are the identical factor.
d) Methodology overriding permits a category to have a number of strategies with the identical title however totally different signatures, whereas methodology overloading permits a baby class to offer a selected implementation of a technique that’s already outlined in its mum or dad class.
Reply: a
Clarification: Methodology overloading entails having a number of strategies with the identical title however totally different parameters inside a category, whereas methodology overriding entails redefining a technique in a subclass that’s already outlined in a mum or dad class.
Q25. What’s methodology decision order (MRO) in Python?
a) MRO defines the order by which strategies are resolved or regarded up within the class hierarchy.
b) MRO is a Python built-in perform to outline strategies.
c) MRO is the method of defining strategies inside a category.
d) MRO will not be related in Python.
Reply: a
Clarification: MRO defines the order by which strategies are resolved or regarded up within the class hierarchy, particularly in instances of a number of inheritance.
Q26. How does Python assist a number of inheritance?
a) Python permits a category to inherit from a number of mum or dad lessons.
b) Python doesn’t assist a number of inheritance.
c) Python solely permits a category to inherit from one mum or dad class.
d) Python randomly selects a mum or dad class when a number of inheritance is used.
Reply: a
Clarification: Python helps a number of inheritance by permitting a category to inherit attributes and strategies from a number of mum or dad lessons.
Q27. What’s the fundamental benefit of utilizing tremendous() in Python?
a) tremendous() permits a baby class to name strategies of the mum or dad class with out explicitly naming them.
b) tremendous() prevents inheritance.
c) tremendous() makes all strategies personal.
d) tremendous() enforces encapsulation of information.
Reply: a
Clarification: The tremendous()
perform permits a baby class to name strategies of the mum or dad class with out explicitly naming the mum or dad class, selling code reusability.
Q28. Which of the next statements about diamond drawback in Python is right?
a) Diamond drawback happens when two youngster lessons inherit from the identical mum or dad class.
b) Diamond drawback happens when a baby class inherits from a number of mum or dad lessons, and each mum or dad lessons have a standard ancestor.
c) Diamond drawback doesn’t exist in Python.
d) Diamond drawback happens when a category has a diamond form in its class hierarchy.
Reply: b
Clarification: The diamond drawback happens in a number of inheritance when a category inherits from two lessons which have a standard ancestor, creating an ambiguous state of affairs for the compiler to resolve.
Q29. What does the del
key phrase do in Python?
a) Deletes a technique from a category
b) Deletes an occasion of a category
c) Deletes an attribute from an occasion of a category
d) Deletes a category from reminiscence
Reply: c
Clarification: The del
key phrase in Python is used to delete an attribute from an occasion of a category.
Q30. What’s the distinction between class variables and occasion variables in Python?
a) Class variables are outlined inside strategies and could be accessed solely by class strategies, whereas occasion variables are outlined exterior strategies and could be accessed by occasion strategies.
b) Class variables are shared amongst all cases of a category, whereas every occasion of a category has its personal copy of occasion variables.
c) Class variables can solely be accessed utilizing the category title, whereas occasion variables can solely be accessed utilizing the occasion title.
d) Class variables are immutable, whereas occasion variables could be modified.
Reply: b
Clarification: Class variables are shared amongst all cases of a category, that means they’re the identical for each occasion. Occasion variables, alternatively, are distinctive to every occasion of a category.
Q31. What’s the goal of the @property
decorator in Python?
a) It permits a technique to be known as with out parentheses.
b) It permits a technique to be overridden in a subclass.
c) It permits a technique to be accessed as an attribute.
d) It permits a technique to be a category methodology.
Reply: c
Clarification: The @property
decorator in Python permits a technique to be accessed like an attribute, offering a cleaner syntax for getters and setters.
Q32. What’s a descriptor in Python?
a) A perform that takes an iterable and returns an iterator
b) A technique that modifies the conduct of an occasion attribute
c) An object attribute with “binding conduct”, one whose attribute entry has been overridden by strategies within the descriptor protocol
d) A particular methodology used for creating class objects
Reply: c
Clarification: A descriptor in Python is an object attribute with “binding conduct”, that means its attribute entry has been overridden by strategies within the descriptor protocol. Descriptors are used to create properties and different managed attributes.
Q33. What does the __new__
methodology do in Python lessons?
a) Initializes the item’s state
b) Creates a brand new object occasion
c) Deletes the item
d) Calls the mum or dad class constructor
Reply: b
Clarification: The __new__
methodology in Python lessons is accountable for creating a brand new object occasion. It’s known as earlier than __init__
and is used to create and return a brand new object.
Q34. What’s the goal of the __slots__
attribute in Python lessons?
a) It specifies the attributes {that a} class occasion can have
b) It restricts the variety of cases that may be created from a category
c) It defines the strategies accessible in a category
d) It specifies the bottom lessons of a category
Reply: a
Clarification: The __slots__
attribute in Python lessons specifies the attributes {that a} class occasion can have. It’s used to optimize reminiscence utilization and limit the attributes of cases.
Congratulations on finishing the Python Inheritance and Polymorphism MCQs! Inheritance and polymorphism are highly effective options in object-oriented programming that promote code reusability and suppleness. By mastering these ideas, you acquire the power to create class hierarchies, specialize lessons, and create extra dynamic and adaptable code. Maintain working towards and experimenting with Python’s inheritance and polymorphism functionalities to turn out to be proficient in constructing strong and extensible functions. When you have any questions or need to delve deeper into any subject, don’t hesitate to proceed your studying journey. Pleased coding!
It’s also possible to enroll in out free Python Course Immediately!
Learn our extra articles associated to MCQs in Python: