Test Series - python

Test Number 107/108

Q: 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
Solution: In multi-level inheritance, a subclass derives from another class which itself is derived from another class.
Q: 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
Solution: 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.
Q: 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
Solution: Any change made in variable i isn’t reflected as it is the private member of the superclass.
Q: 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
Solution: If the value of a private variable in a superclass is changed in the subclass, the change isn’t reflected.
Q: What will be the output of the following Python code?

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
Solution: Initially x=3 and y=0. When obj.count() is called, y=1.
Q: 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
Solution: isinstance(obj,class) returns True if obj is an object class.
Q: 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
Solution: The __eq(other) method is called if any comparison takes place and it is defined in the object class.
Q: Method issubclass() checks if a class is a subclass of another class.
A. True
B. False
C. none
D. ..
Solution: Method issubclass() returns True if a class is a subclass of another class and False otherwise.
Q: 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
Solution: Private class members in the superclass can’t be accessed in the subclass.
Q: 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. Error, private class member can’t be accessed in a subclass
D. 5
Solution: The class member x is protected, not private and hence can be accessed by subclasses.

You Have Score    /10