Python Inheritance Quiz

Python Inheritance Quiz

Pre-quiz Inheritance


Q1 – An object is created using __________ .




Q2 – _____ is a python special method used to initialize the values of instance members for the new object.




Q3 – _________ is a used to access the attributes and methods of the class in python.




Q4 – A class can serve as base class for many derived classes.





Practice Session – Inheritance


Q1 – Consider class C in a multiple inheritance hierarchy and inherits from classes B and A, write a statement which returns order in which Python looks for a method in a hierarchy of classes for class C as tuple?

C.__mro__


Q2 – Assume you have 3 classes A,B and C, ll in the following code to achieve multilevel inheritance class A:   Pass class B(A):   Pass

class C(B):

  Pass


Q3 – Suppose Student class derived from the class Person, write a piece of code to invoke the __init__ method in Person from Student?

Person.__init__(self)



Post Quiz – Inheritance


Q1 – Which of the following statements are true about inheritance




Q2 – More than one derived class are created from a single base class is known as




Q3 – Super() class method is used to




Q4 – In case of multiple inheritance, any particular attribute is searched rst in current class. if not found, it searches into parent class in




Q5 – _____ ensures a class always appears before its parent and in case of multiple parents, the order is same as tuple of base classes.





Check Your Understanding – Inheritance


Q1 – Match the following

Single Inheritance

a child class inherits all the properties of a parent class

Multilevel Inheritance

a derived class will inherit a base class and as well as the derived class also act as the base class to other class

Multiple Inheritance

a child class inherits the properties of more than one parent class

Hierarchical Inheritance

More than one child classes inherit all the properties of the same parent class.


Q2 – Predict the output of following piece of code
class x:
  pass
class Y(x):
  pass
class Z(x):
  pass
class F(A, B):
  pass
print(C.__mro__)
print(C.mro())




Q3 – Re-arrange the given code to get output given below:
Output should be like:
Employee
Id: 1
Name: Ashrath
Company: PSG
Doctor
Id: 1
Name: Arrif
Hospital: APPOLO

class Details:
def __init__(self,id,name):
self.id=id
self.name=name

def display(self):
print(“Id: “,self.id)
print(“Name: “, self.name)

class Employee(Details):
def __init__(self,id,name,comp):
Details.__init__(self,id,name)
self.comp=comp

def displayE(self):
Details.display(self)
print(“Company: “, self.comp)

class Doctor(Details):
def __init__(self,id,name,hos):

Details.__init__(self,id,name)

self.hos=hos

def displayD(self):
Details.display(self)
print(“Hospital: “, self.hos)

print(“Employee”)
emp=Employee(1,’Ashrath’,’PSG’)
emp.displayE()
print(“Doctor”)
doc = Doctor(1,”Arrif”,”APPOLO”)
doc.displayD()


Q4 – By Inheritance the members of parent class can be reused by all the subclass




Q5 – Predict the output of the following piece of code
class X(object):
  def display(self):
   print(‘X’)
class Y(object):
  def display(self):
   print(‘Y’)
class Z(object):
  def display(self):
   print(‘Z’)
class A(X, Y):
  def display1(self):
   print(‘A’)
class B(Y, Z):
  def display1(self):
   print(‘B’)
class M(B,A):
  pass

obj=M()
print(obj.display())




Recommended :

  1. Saving Bank Account
  2. Student Information
  3. Maintain Project Information
  4. Classic Curve Members
  5. Faculties and Students

Leave a Comment

Your email address will not be published. Required fields are marked *

x