Python MCQs on Inheritance: SET 1

Python MCQs on Inheritance

In this section, we are going to introduce you to the most frequently asked Python MCQs on Inheritance. These Python MCQs on Inheritance will be going to help you in Interviews and Exams.

 

 

1. Which of the following best describes inheritance?

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 implementation of elegant software that is well designed and easily modified

Check Answer

Answer: a
If the class definition is class B(A): then class B inherits the methods of class A. This is called inheritance.

2. Which of the following statements is wrong about inheritance?

a) Protected members of a class can be inherited

b) The inheriting class is called a subclass

c) Private members of a class can be inherited and accessed

d) Inheritance is one of the features of OOP

Check Answer

Answer: c
Any changes made to the private members of the class in the subclass aren’t reflected in the original members.

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

class Demo:

def __new__(self):

self.__init__(self)

print(“Demo’s __new__() invoked”)

def __init__(self):

print(“Demo’s __init__() invoked”)

class Derived_Demo(Demo):

def __new__(self):

print(“Derived_Demo’s __new__() invoked”)

def __init__(self):

print(“Derived_Demo’s __init__() invoked”)

def main():

obj1 = Derived_Demo()

obj2 = Demo()

main()

a)

Derived_Demo’s __init__() invoked

Derived_Demo’s __new__() invoked

Demo’s __init__() invoked

Demo’s __new__() invoked

b)

Derived_Demo’s __new__() invoked

Demo’s __init__() invoked

Demo’s __new__() invoked

c)

Derived_Demo’s __new__() invoked

Demo’s __new__() invoked

d)

Derived_Demo’s __init__() invoked

Demo’s __init__() invoked

Check Answer

Answer: b
Since the object for the derived class is declared first, __new__() method of the derived class is invoked first, followed by the constructor and the __new__() method of main class.

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

class Test:

def __init__(self):

self.x = 0

class Derived_Test(Test):

def __init__(self):

self.y = 1

def main():

b = Derived_Test()

print(b.x,b.y)

main()

a) 0 1

b) 0 0

c) Error because class B inherits A but variable x isn’t inherited

d) Error because when object is created, argument must be passed like Derived_Test(1)

Check Answer

Answer: c
Since the invoking method, Test.__init__(self), isn’t present in the derived class, variable x can’t be inherited.

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

class A():

def disp(self):

print(“A disp()”)

class B(A):

pass

obj = B()

obj.disp()

a) Invalid syntax for inheritance

b) Error because when object is created, argument must be passed

c) Nothing is printed

d) A disp()

Check Answer

Answer: d
Class B inherits class A hence the function disp () becomes part of class B’s definition. Hence disp() method is properly executed and the line is printed.

6. All subclasses are a subtype in object-oriented programming.

a) True

b) False

Check Answer

Answer: b
A subtype is something that be substituted for and behave as its parent type. All subclass may not be a subtype in object-oriented programming.

7. When defining a subclass in Python that is meant to serve as a subtype, the subtype Python keyword is used.

a) True

b) False

Check Answer

Answer: b
B is a subtype of B if instances of type B can substitute for instances of type A without affecting semantics.

8. Suppose B is a subclass of A, to invoke the __init__ method in A from B, what is the line of code you should write?

a) A.__init__(self)

b) B.__init__(self)

c) A.__init__(B)

d) B.__init__(A)

Check Answer

Answer: a
To invoke the __init__ method in A from B, either of the following should be written: A.__init__(self) or super().__init__(self).

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

class Test:

def __init__(self):

self.x = 0

class Derived_Test(Test):

def __init__(self):

Test.__init__(self)

self.y = 1

def main():

b = Derived_Test()

print(b.x,b.y)

main()

a) Error because class B inherits A but variable x isn’t inherited

b) 0 0

c) 0 1

d) Error, the syntax of the invoking method is wrong

Check Answer

Answer: c
Since the invoking method has been properly invoked, variable x from the main class has been properly inherited and it can also be accessed.

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

class A:

def __init__(self, x= 1):

self.x = x

class der(A):

def __init__(self,y = 2):

super().__init__()

self.y = y

def main():

obj = der()

print(obj.x, obj.y)

main()

a) Error, the syntax of the invoking method is wrong

b) The program runs fine but nothing is printed

c) 1 0

d) 1 2

Check Answer

Answer: d
In the above piece of code, the invoking method has been properly implemented and hence x=1 and y=2.

Page  1  2  3

 

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

 

Leave a Comment