Python Programming Multiple Choice Question -
Inheritance
This section focuses on the "Inheritance" in Python programming. These Multiple Choice Questions (mcq) should be practiced to improve the Python programming skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.
1. What is true about Inheritance in Python?
A. Inheritance is the capability of one class to derive or inherit the properties from another class.
B. It represents real-world relationships well.
C. It provides reusability of a code.
D. All of the above
View Answer
Ans : D
Explanation: All of the above statement are true.
2. When a child class inherits from only one parent class, it is called?
A. single inheritance
B. singular inheritance
C. Multiple inheritance
D. Multilevel inheritance
View Answer
Ans : A
Explanation: Single inheritance: When a child class inherits from only one parent class, it is called single inheritance.
3. Which inheritance is a blend of more than one type of inheritance?
A. single inheritance
B. Hybrid inheritance
C. Multiple inheritance
D. Multilevel inheritance
View Answer
Ans : B
Explanation: Hybrid inheritance: This form combines more than one form of inheritance. Basically, it is a blend of more than one type of inheritance.
4. Parent class is the class being inherited from, also called?
A. derived class
B. Child class
C. Hybrid class
D. base class
View Answer
Ans : D
Explanation: Parent class is the class being inherited from, also called base class.
5. The child's __init__() function overrides the inheritance of the parent's __init__() function.
A. TRUE
B. FALSE
C. Can be true or false
D. Can not say
View Answer
Ans : A
Explanation: The child's __init__() function overrides the inheritance of the parent's __init__() function.
6. __________function that will make the child class inherit all the methods and properties from its parent
A. self
B. __init__()
C. super
D. pass
View Answer
Ans : C
Explanation: Python also has a super() function that will make the child class inherit all the methods and properties from its parent
7. 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
Ans : A
Explanation: To invoke the __init__ method in A from B, either of the following should be written: A.__init__(self) or super().__init__(self).
8. 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
Ans : B
Explanation: For example: >>> type((1,)) gives .
9. Which of the following statements is false?
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
Ans : C
Explanation: If the value of a private variable in a superclass is changed in the subclass, the change isn’t reflected.
10. What will be output for the folllowing 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
Ans : D
Explanation: In the above piece of code, the invoking method has been properly implemented and hence x=1 and y=2.
Discussion