This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Inheritance”.
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
View Answer
Explanation: 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
View Answer
Explanation: 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__() invokedView Answer
Explanation: 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)
View Answer
Explanation: 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()
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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)
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: In the above piece of code, the invoking method has been properly implemented and hence x=1 and y=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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: In multiple inheritance, two or more subclasses are derived from the superclass as shown in the above piece of code.
More MCQs on Python Inheritance:
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check Information Technology Books
- Apply for Python Internship
- Practice Programming MCQs
- Apply for Programming Internship
- Check Python Books