Python Programming Quiz

Quiz on Inheritance in Python

Time left: 300 seconds

Q.1. Which of the following statements is wrong about inheritance?

Select Your Option

Any changes made to the private members of the class in the subclass aren't reflected in the original members.

Q.2. What is Overriding

Select Your Option

Overriding is when we have an inherited method in the child with the same name as a method of the parent class but with different functionality.

Q.3. What is the output of following program?
          
            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()
          
          

Select Your Option

Since the invoking method, Test.__init__(self), isn't present in the derived class, variable x can't be inherited.

Q.4. 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?

Select Your Option

To invoke the __init__ method in A from B, either of the following should be written: A.__init__(self) or super().__init__(self).

Q.5. When defining a subclass in Python that is meant to serve as a subtype, the subtype Python keyword is used.

Select Your Option

B is a subtype of B if instances of type B can substitute for instances of type A without affecting semantics

Q.6. What type of inheritance is illustrated in the following Python code?
          
            class A():
                pass
            class B(A):
                pass
            class C(A):
                pass
          
          

Select Your Option

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

Q.7. 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()
          
          

Select Your Option

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.

Q.8. The child's __init__() function overrides the inheritance of the parent's __init__() function.

Select Your Option

The child's __init__() function overrides the inheritance of the parent's __init__() function.

Q.9. Which of the following is not a type of inheritance?

Select Your Option

Multiple, multi-level, single-level and hierarchical inheritance are all types of inheritance.

Q.10. Method issubclass() checks if a class is a subclass of another class.

Select Your Option

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

Q.11. What does built-in function help do in context of classes?

Select Your Option

help() usually gives information of the class on any built-in type or function.

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

Select Your Option

In the above piece of code, the invoking method has been properly implemented and hence x=1 and y=2.

Q.13. What does built-in function type() do in context of classes?

Select Your Option

For example: >>> type((1,)) gives .

Q.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())
          
          

Select Your Option

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’.

Q.15. In order to extend a class, the new class should have access to all the data and inner workings of the parent class.

Select Your Option

The child class knows how to use the parent class and its functions but not its data and the inner workings. The "super" command can come in handy here.

Instructions:-

  1. This Quiz is based on Inheritance in Python
  2. Each correct answer will carry 2 points
  3. Once an option is selected you cannot select any other, So choose wisely
  4. Do not refresh the page as it will reset the quiz
  5. Once the time is up the quiz will automatically get submitted with no of Question responded
  6. Your total score alongwith your name will be shown after submission of the Quiz
  7. Wish you ALL THE BEST 👍👍

START

Results:-

Hi




Post a Comment

FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP

Previous Post Next Post