Python MCQs on Inheritance: SET 3

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

>>> class A:

pass

>>> class B(A):

pass

>>> obj=B()

>>> isinstance(obj,A)

a) True

b) False

c) Wrong syntax for isinstance() method

d) Invalid method for classes

Check Answer

Answer: a
isinstance(obj,class) returns True if obj is an object class.

22. Which of the following statements is true?

a) The __new__() method automatically invokes the __init__ method

b) The __init__ method is defined in the object class

c) The __eq(other) method is defined in the object class

d) The __repr__() method is defined in the object class

Check Answer

Answer: c
The __eq(other) method is called if any comparison takes place and it is defined in the object class.

23. Method issubclass() checks if a class is a subclass of another class.

a) True

b) False

Check Answer

Answer: a
Method issubclass() returns True if a class is a subclass of another class and False otherwise.

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

class A:

def __init__(self):

self.__x = 1

class B(A):

def display(self):

print(self.__x)

def main():

obj = B()

obj.display()

main()

a) 1

b) 0

c) Error, invalid syntax for object declaration

d) Error, private class member can’t be accessed in a subclass

Check Answer

Answer: d
Private class members in the superclass can’t be accessed in the subclass.

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

class A:

def __init__(self):

self._x = 5

class B(A):

def display(self):

print(self._x)

def main():

obj = B()

obj.display()

main()

a) Error, invalid syntax for object declaration

b) Nothing is printed

c) 5

d) Error, private class member can’t be accessed in a subclass

Check Answer

Answer: c
The class member x is protected, not private and hence can be accessed by subclasses.

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

class A:

def __init__(self,x=3):

self._x = x

class B(A):

def __init__(self):

super().__init__(5)

def display(self):

print(self._x)

def main():

obj = B()

obj.display()

main()

a) 5

b) Error, class member x has two values

c) 3

d) Error, protected class member can’t be accessed in a subclass

Check Answer

Answer: a
The super() method re-assigns the variable x with value 5. Hence 5 is printed.

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

class A:

def test1(self):

print(” test of A called “)

class B(A):

def test(self):

print(” test of B called “)

class C(A):

def test(self):

print(” test of C called “)

class D(B,C):

def test2(self):

print(” test of D called “)

obj=D()

obj.test()

a)

test of B called

test of C called

b)

test of C called

test of B called

c) test of B called

d) Error, both the classes from which D derives has same method test()

Check Answer

Answer: c
Execute in Python shell to verify. If class D(B,C): is switched is class D(C,B): test of C is called.

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

class A:

def test(self):

print(“test of A called”)

class B(A):

def test(self):

print(“test of B called”)

super().test()

class C(A):

def test(self):

print(“test of C called”)

super().test()

class D(B,C):

def test2(self):

print(“test of D called”)

obj=D()

obj.test()

a)

test of B called

test of C called

test of A called

b)

test of C called

test of B called

c)

test of B called

test of C called

d) Error, all the three classes from which D derives has same method test()

Check Answer

Answer: a
Since the invoking method, super().test() is called in the subclasses, all the three methods of test() in three different classes is called.

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