Python Programming Quiz

Quiz on Polymorphism in Python

Time left: 300 seconds

Q.1. Which of the following best describes polymorphism?

Select Your Option

Polymorphism is a feature of object-oriented programming languages. It allows for the implementation of elegant software that is well designed and easily modified.

Q.2. What is the main motive behind the use of polymorphism?

Select Your Option

Polymorphism allows for the implementation of elegant software.

Q.3. What do you call the languages that support classes but not polymorphism?

Select Your Option

The languages which support classes but doesn’t support polymorphism, are known as object-based languages. Polymorphism is such an important feature, that is a language doesn’t support this feature, it can’t be called as a OOP language.

Q.4. What type of inheritance is illustrated in the following Python code?
          
            class A:
                def __str__(self):
                    return '1'
            class B(A):
                def __init__(self):
                    super().__init__()
            class C(B):
                def __init__(self):
                    super().__init__()
            def main():
                obj1 = B()
                obj2 = A()
                obj3 = C()
                print(obj1, obj2,obj3)
            main()
          
          

Select Your Option

The super().__init__() in the subclasses has been properly invoked and none of other subclasses return any other value. Hence 1 is returned each time the object is created and printed.

Q.5. What type of inheritance is illustrated in 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'
            obj2=B()
            print(obj2.two())
          
          

Select Your Option

obj2 is instance of class B and here two() function is Overriding. Hence, the function two() of class B is executed.

Q.6. Which of the following statements is true?

Select Your Option

A public method in the base class can be overridden by the same named method in the subclass.

Q.7. What will be the output of the following Python code?
          
            class A:
                def __init__(self, x, y):
                    self.x = x
                    self.y = y
                def __str__(self):
                    return 1
                def __eq__(self, other):
                    return self.x * self.y == other.x * other.y
            obj1 = A(5, 2)
            obj2 = A(2, 5)
            print(obj1 == obj2)
          
          

Select Your Option

Since 5*2==2*5, True is printed. Execute it in the Python shell to verify.

Q.8. A class in which one or more methods are only implemented to raise an exception is called an abstract class.

Select Your Option

A class in which one or more methods are unimplemented or implemented for the methods throw an exception is called an abstract class.

Q.9. What will be the output of the following Python code?
          
            class Demo:
                def __init__(self):
                    self.x = 1
                def change(self):
                    self.x = 10
            class Demo_derived(Demo):
                def change(self):
                    self.x=self.x+1
                    return self.x
            def main():
                obj = Demo_derived()
                print(obj.change())
 
            main()
          
          

Select Your Option

The derived class method change() overrides the base class method.

Q.10. What is the use of duck typing?

Select Your Option

In Python, any set of classes with a common set of methods can be treated similarly. This is called duck typing. Hence duck typing imposes less restrictions.

Q.11. Which class/set of classes can illustrate polymorphism in the following code?
          
            abstract class student
            {
                public : int marks;
                calc_grade();
            }
            class topper:public student
            {
                public : calc_grade()
                { 
                    return 10; 
                }
            };
            class average:public student
            { 
                public : calc_grade()
                {
                    return 20; 
                }
            };
            class failed{ int marks; };
            
          
          

Select Your Option

Since Student class is abstract class and class topper and average are inheriting student, class topper and average must define the function named calc_grade(); in abstract class. Since both the definition are different in those classes, calc_grade() will work in different way for same input from different objects. Hence it shows polymorphism.

Q.12. Overriding means changing behaviour of methods of derived class methods in the base class.

Select Your Option

Overriding means if there are two same methods present in the superclass and the subclass, the contents of the subclass method are executed.

Q.13. Which among the following can’t be used for polymorphism?

Select Your Option

Static member functions are not property of any object. Hence it can’t be considered for overloading/overriding. For polymorphism, function must be property of object, not only of class.

Q.14. Which among the following can show polymorphism?

Select Your Option

Only insertion operator can be overloaded among all the given options. And the polymorphism can be illustrated here only if any of these is applicable of being overloaded. Overloading is type of polymorphism.

Q.15. Which problem may arise if we use abstract class functions for polymorphism?

Select Your Option

The undefined functions must be defined is a problem, because one may need to implement few undefined functions from abstract class, but he will have to define each of the functions declared in abstract class. Being useless task, it is a problem sometimes.

Instructions:-

  1. This Quiz is based on Polymorphism 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