Method Overloading And Overriding
1.What happens if two overloaded methods have the same name and parameters but different return types?
-
A. The compiler treats them as different methods
-
B. The method with a larger return type is chosen
-
C. Compilation error occurs
-
D. The method with a void return type is preferred
Compilation error occurs
Compilation error occurs
2.What is required for method overriding to work?
-
A. Methods must have the same name, parameters, and return type
-
B. The superclass method must be static
-
C. The method in the subclass must have a different number of parameters
-
D. The subclass method must have a different return type
Methods must have the same name, parameters, and return type
Methods must have the same name, parameters, and return type
3.Which method in Java cannot be overridden?
-
A. A public method
-
B. A static method
-
C. A private method
-
D. Both B and C
Both B and C
Both B and C
4.What will happen if a superclass method is declared final and a subclass tries to override it?
-
A. The subclass method executes normally
-
B. Compilation error occurs
-
C. The subclass method is ignored at runtime
-
D. The subclass method can only execute using super
Compilation error occurs
Compilation error occurs
5.How does method overloading improve code readability in real-world applications?
-
A. By allowing multiple methods with the same name to perform different tasks
-
B. By reducing the number of methods in a class
-
C. By forcing the use of default values for method parameters
-
D. By making sure all methods return the same type
By allowing multiple methods with the same name to perform different tasks.
By allowing multiple methods with the same name to perform different tasks.