Python Questions and Answers – Inheritance – 2

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Inheritance – 2”.

1. 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
View Answer

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

2. 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
View Answer

Answer: c
Explanation: 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.

3. 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
View Answer

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

4. 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
View Answer

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

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

advertisement
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
View Answer

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

6. 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
View Answer

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

7. 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
View Answer

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

8. Method issubclass() checks if a class is a subclass of another class.
a) True
b) False
View Answer

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

9. 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
View Answer

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

10. 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
View Answer

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

11. 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
View Answer

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

12. 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()
View Answer

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

13. 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()
View Answer

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

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.