Python MCQs on Inheritance: SET 2

11. What does built-in function type do in context of classes?

a) Determines the object name of any value

b) Determines the class name of any value

c) Determines class description of any value

d) Determines the file name of any value

Check Answer

Answer: b
For example: >>> type((1,)) gives <class ‘tuple’>.

12. Which of the following is not a type of inheritance?

a) Double-level

b) Multi-level

c) Single-level

d) Multiple

Check Answer

Answer: a
Multiple, multi-level, single-level and hierarchical inheritance are all types of inheritance.

13. What does built-in function help do in context of classes?

a) Determines the object name of any value

b) Determines the class identifiers of any value

c) Determines class description of any built-in type

d) Determines class description of any user-defined built-in type

Check Answer

Answer: c
help() usually gives information of the class on any built-in type or function.

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’

obj1=A()

obj2=B()

print(obj1.two(),obj2.two())

a) A A

b) A B

c) B B

d) An exception is thrown

Check Answer

Answer: b
obj1.two() invokes the method two() in class A which returns ‘A’ and obj2.two() invokes the method two() in class B which returns ‘B’.

15. What type of inheritance is illustrated in the following Python code?

class A():

pass

class B():

pass

class C(A,B):

pass

a) Multi-level inheritance

b) Multiple inheritance

c) Hierarchical inheritance

d) Single-level inheritance

Check Answer

Answer: b
In multiple inheritance, two or more subclasses are derived from the superclass as shown in the above piece of code.

16. What type of inheritance is illustrated in the following Python code?

class A():

pass

class B(A):

pass

class C(B):

pass

a) Multi-level inheritance

b) Multiple inheritance

c) Hierarchical inheritance

d) Single-level inheritance

Check Answer

Answer: a
In multi-level inheritance, a subclass derives from another class which itself is derived from another class.

17. What does single-level inheritance mean?

a) A subclass derives from a class which in turn derives from another class

b) A single superclass inherits from multiple subclasses

c) A single subclass derives from a single superclass

d) Multiple base classes inherit a single derived class

Check Answer

Answer: c
In single-level inheritance, there is a single subclass which inherits from a single superclass. So the class definition of the subclass will be: class B(A): where A is the superclass.

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

class A:

def __init__(self):

self.__i = 1

self.j = 5

def display(self):

print(self.__i, self.j)

class B(A):

def __init__(self):

super().__init__()

self.__i = 2

self.j = 7

c = B()

c.display()

a) 2 7

b) 1 5

c) 1 7

d) 2 5

Check Answer

Answer: c
Any change made in variable i isn’t reflected as it is the private member of the superclass.

19. Which of the following statements isn’t true?

a) A non-private method in a superclass can be overridden

b) A derived class is a subset of superclass

c) The value of a private variable in the superclass can be changed in the subclass

d) When invoking the constructor from a subclass, the constructor of superclass is automatically invoked

Check Answer

Answer: c
If the value of a private variable in a superclass is changed in the subclass, the change isn’t reflected.

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

class A:

def __init__(self,x):

self.x = x

def count(self,x):

self.x = self.x+1

class B(A):

def __init__(self, y=0):

A.__init__(self, 3)

self.y = y

def count(self):

self.y += 1

def main():

obj = B()

obj.count()

print(obj.x, obj.y)

main()

a) 3 0

b) 3 1

c) 0 1

d) An exception in thrown

Check Answer

Answer: b
Initially x=3 and y=0. When obj.count() is called, y=1.

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