a) The ability of a variable to hold different data types
b) The concept of allowing methods to perform different tasks based on the object
c) The process of changing the state of an object
d) The ability to define multiple variables in a single declaration
Answer:
b) The concept of allowing methods to perform different tasks based on the object
Explanation:
Polymorphism in Java allows methods to perform different tasks based on the object that invokes them, enhancing flexibility and reusability.
2. How is polymorphism typically achieved in Java?
a) Through the use of interfaces
b) By overloading methods
c) By overriding methods
d) Both b and c
Answer:
d) Both b and c
Explanation:
Polymorphism in Java is commonly achieved through method overloading (compile-time polymorphism) and method overriding (runtime polymorphism).
3. What is method overloading in Java?
a) Redefining a method in a subclass
b) Changing the return type of a method
c) Creating multiple methods with the same name but different parameters in the same class
d) Extending a method from a superclass
Answer:
c) Creating multiple methods with the same name but different parameters in the same class
Explanation:
Method overloading, a form of compile-time polymorphism, involves defining multiple methods with the same name but different parameter lists in the same class.
4. What is method overriding in Java?
a) Providing a new implementation for an inherited method in a subclass
b) Creating a method with the same name and parameters in the same class
c) Changing the return type of a method in a subclass
d) Defining a method in an interface
Answer:
a) Providing a new implementation for an inherited method in a subclass
Explanation:
Method overriding, a form of runtime polymorphism, occurs when a subclass provides a specific implementation for a method already defined in its superclass.
5. Which keyword is used to achieve runtime polymorphism in Java?
a) static
b) final
c) super
d) override
Answer:
c) super
Explanation:
The 'super' keyword is used in method overriding, which is a mechanism of runtime polymorphism, to refer to the method defined in the superclass.
6. Can a static method be overridden in Java?
a) Yes
b) No
c) Only if it's in a superclass
d) Only if it's in a subclass
Answer:
b) No
Explanation:
Static methods belong to the class, not instances, and cannot be overridden. They can be hidden by a method in a subclass with the same signature.
7. What is dynamic method dispatch in Java?
a) Compiling multiple methods at once
b) Deciding at runtime which method to call
c) Overloading methods in runtime
d) Dispatching methods to different classes
Answer:
b) Deciding at runtime which method to call
Explanation:
Dynamic method dispatch is a mechanism in Java where the method to be executed is determined at runtime, a key aspect of runtime polymorphism.
8. What is the main advantage of polymorphism in Java?
a) It increases the execution speed of programs
b) It allows methods to be called without specifying the exact class type
c) It simplifies code maintenance and readability
d) It reduces the size of the code
Answer:
c) It simplifies code maintenance and readability
Explanation:
Polymorphism enhances code maintainability and readability by allowing the same method to be used on different objects, reducing the complexity of the code.
9. What does polymorphism mean in terms of OOP principles?
a) Single inheritance
b) Multiple methods with the same name
c) Many forms or the ability to take on different forms
d) Encapsulation of methods and variables
Answer:
c) Many forms or the ability to take on different forms
Explanation:
Polymorphism in OOP principles refers to the ability of an entity, such as methods or objects, to take on many forms.
10. How does polymorphism benefit code reusability?
a) By using the same method for different purposes
b) By copying methods from one class to another
c) By allowing methods to run faster
d) By reducing the amount of memory used by the program
Answer:
a) By using the same method for different purposes
Explanation:
Polymorphism promotes code reusability by allowing the same method to be used for different purposes on different objects.
11. In Java, which type of polymorphism is supported by method overloading?
a) Runtime polymorphism
b) Compile-time polymorphism
c) Dynamic polymorphism
d) Static polymorphism
Answer:
b) Compile-time polymorphism
Explanation:
Method overloading is an example of compile-time polymorphism, where the method to be executed is determined by the compiler based on the method signature.
12. Can interfaces in Java be used to achieve polymorphism?
a) Yes
b) No
c) Only if they are abstract
d) Only if they extend a class
Answer:
a) Yes
Explanation:
Interfaces in Java are used to achieve polymorphism. A class can implement an interface and provide its own implementation of the interface's methods, allowing objects to take multiple forms.