1. What is method overriding in Java?
a) Renaming a method in a subclass
b) Changing the return type of a method in a subclass
c) Defining a method in a subclass that already exists in the superclass
d) Creating a new method in a subclass that does not exist in the superclass
Answer:
c) Defining a method in a subclass that already exists in the superclass
Explanation:
Method overriding in Java occurs when a subclass provides its own specific implementation for a method that already exists in its superclass.
2. What is required for a method to be overridden?
a) The method must be static
b) The method must have a different name
c) The method must have the same name and same parameter list
d) The method must have the same name but a different parameter list
Answer:
c) The method must have the same name and same parameter list
Explanation:
For a method to be overridden in Java, it must have the same name and the same parameter list as the method in the superclass.
3. Can private methods be overridden in Java?
a) Yes
b) No
c) Only if they are static
d) Only if they are final
Answer:
b) No
Explanation:
Private methods in Java cannot be overridden because they are not accessible to subclasses.
4. Which keyword is used to call an overridden method from a subclass in Java?
a) this
b) super
c) extend
d) override
Answer:
b) super
Explanation:
The 'super' keyword is used in a subclass to call the overridden method from the superclass.
5. What is dynamic method dispatch in Java?
a) Calling a static method of a class
b) Deciding which method to call at compile time
c) Resolving a method call at runtime
d) Changing the method signature at runtime
Answer:
c) Resolving a method call at runtime
Explanation:
Dynamic method dispatch is a runtime polymorphism feature in Java where the call to an overridden method is resolved at runtime.
6. Can an overridden method change the access level of the base method?
a) Yes, to any level
b) No, it must be the same
c) Yes, but it cannot be more restrictive
d) Yes, but it must be more restrictive
Answer:
c) Yes, but it cannot be more restrictive
Explanation:
An overridden method in Java can have a less restrictive access modifier than the method in the superclass but cannot be more restrictive.
7. Can a final method be overridden in Java?
a) Yes
b) No
c) Only if it's also static
d) Only in the same package
Answer:
b) No
Explanation:
Final methods in Java cannot be overridden as they are declared to prevent any modification.
8. What happens if the 'super' keyword is not used in an overridden method?
a) The superclass method is automatically called
b) An error occurs
c) The overridden method will not execute
d) Only the subclass's version of the method is executed
Answer:
d) Only the subclass's version of the method is executed
Explanation:
If 'super' is not used, the overridden method in the subclass will execute without calling the superclass version of the method.
9. Why is method overriding used in Java?
a) To improve the readability of the code
b) To allow the subclass to provide a specific implementation of a method
c) To increase the efficiency of the code
d) To prevent changes in the superclass method
Answer:
b) To allow the subclass to provide a specific implementation of a method
Explanation:
Method overriding is used in Java to allow a subclass to provide its own specific implementation of a method already defined in its superclass.
10. What is the significance of @Override annotation in Java?
a) It changes the behavior of the overridden method
b) It indicates that a method is being overridden
c) It is mandatory for method overriding
d) It improves the performance of the method
Answer:
b) It indicates that a method is being overridden
Explanation:
The @Override annotation is used in Java to indicate that a method is intended to override a method in a superclass.
11. Can constructors be overridden in Java?
a) Yes
b) No
c) Only if they are public
d) Only if they are protected
Answer:
b) No
Explanation:
Constructors cannot be overridden in Java as they are not inherited by subclasses.
12. Is it possible to override a method in the same class?
a) Yes
b) No
c) Only if the method is static
d) Only if the method is private
Answer:
b) No
Explanation:
Overriding a method requires having a method with the same signature in a subclass. It is not possible to override a method within the same class.