What is an advantage of polymorphism?

  1. The same program logic can be used with objects of several related types.

  2. Variables can be re-used in order to save memory.

  3. Constructing new objects from old objects of a similar type saves time.

  4. Polymorphism is a dangerous aspect of inheritance and should be avoided.


Correct Option: A

AI Explanation

To answer this question, you need to understand the concept of polymorphism.

Polymorphism is a fundamental concept in object-oriented programming that allows objects of different classes to be treated as objects of a common superclass. It enables the same code or program logic to be used with objects of several related types.

Let's go through each option to understand why it is correct or incorrect:

Option A) The same program logic can be used with objects of several related types. - This option is correct. Polymorphism allows objects of different classes to be treated as objects of a common superclass. This means that the same program logic or code can be written to operate on objects of different classes, as long as they are related through inheritance. This promotes code reusability and flexibility.

Option B) Variables can be re-used in order to save memory. - This option is incorrect. Polymorphism is not directly related to variable reusability or memory saving. Polymorphism focuses on the behavior and interaction of different objects, rather than the reuse of variables.

Option C) Constructing new objects from old objects of a similar type saves time. - This option is incorrect. Constructing new objects from old objects of a similar type is not directly related to polymorphism. This concept is known as object cloning or object copying, and it is not a unique advantage of polymorphism.

Option D) Polymorphism is a dangerous aspect of inheritance and should be avoided. - This option is incorrect. Polymorphism is not a dangerous aspect of inheritance. In fact, it is one of the powerful features of object-oriented programming that promotes code reusability, flexibility, and extensibility.

The correct answer is A) The same program logic can be used with objects of several related types. This option is correct because polymorphism allows objects of different classes to be treated as objects of a common superclass, enabling the same program logic or code to be used with objects of different classes.

Find more quizzes: