1. What is method overloading in Java?
Answer:
Explanation:
Method overloading in Java is the process of defining more than one method with the same name in a class, but with different parameter lists.
2. Which aspect of a method is considered in method overloading?
Answer:
Explanation:
Method overloading is determined by the number and type of parameters in the method signature, not by the return type or access modifier.
3. Can overloaded methods have different return types in Java?
Answer:
Explanation:
Overloaded methods in Java can have different return types, provided they have different parameter lists.
4. Is it possible to overload a method by changing only its return type?
Answer:
Explanation:
Overloading a method requires a change in the parameter list. Changing only the return type is not sufficient for method overloading.
5. What is the main advantage of method overloading?
Answer:
Explanation:
Method overloading improves code readability and reusability by allowing different implementations under the same method name based on different parameters.
6. Which of these is a correct example of method overloading?
Answer:
Explanation:
Both examples show different parameter lists (either in number or type), which is a valid case of method overloading.
7. Can constructors in Java be overloaded?
Answer:
Explanation:
Constructors in Java can be overloaded, similar to methods, by having different sets of parameters.
8. What happens if two overloaded methods have the same parameters but different return types?
Answer:
Explanation:
In Java, methods cannot be overloaded by return type alone. If two methods have the same name and parameters but different return types, it will result in a compile-time error.
9. How does method overloading support polymorphism?
Answer:
Explanation:
Method overloading is a form of compile-time polymorphism where the method to be executed is determined by the compiler based on the method's signature.
10. Can method overloading be achieved by changing the order of parameters?
Answer:
Explanation:
Method overloading can be achieved by changing the order of parameters, especially if the parameters are of different types.
11. Why is method overloading not possible across different classes in Java?
Answer:
Explanation:
Method overloading is specific to the class in which the methods are defined. Overloading implies multiple methods with the same name in the same class.
12. What is the role of the 'this' keyword in method overloading?
Answer:
Explanation:
The 'this' keyword in method overloading can be used to call another constructor (overloaded method) within the same class or to distinguish between instance variables and parameters when they have the same name.