Test Series - python

Test Number 106/108

Q: All subclasses are a subtype in object-oriented programming.
A. True
B. False
C. none
D. ..
Solution: 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.
Q: When defining a subclass in Python that is meant to serve as a subtype, the subtype Python keyword is used.
A. True
B. False
C. none
D. ..
Solution: B is a subtype of B if instances of type B can substitute for instances of type A without affecting semantics.
Q: 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)
Solution: To invoke the __init__ method in A from B, either of the following should be written: A.__init__(self) or super().__init__(self).
Q: 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
Solution: Since the invoking method has been properly invoked, variable x from the main class has been properly inherited and it can also be accessed.
Q: 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
Solution: In the above piece of code, the invoking method has been properly implemented and hence x=1 and y=2.
Q: 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
Solution: For example: >>> type((1,)) gives .
Q: Which of the following is not a type of inheritance?
A. Double-level
B. Multi-level
C. Single-level
D. Multiple
Solution: Multiple, multi-level, single-level and hierarchical inheritance are all types of inheritance.
Q: 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
Solution: help() usually gives information of the class on any built-in type or function.
Q: 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
Solution: In multiple inheritance, two or more subclasses are derived from the superclass as shown in the above piece of code.

You Have Score    /9