Java Method Overloading MCQ Questions and Answers

1. What is method overloading in Java?

a) Defining multiple methods with the same name in different classes
b) Defining a method in a subclass with the same name as the superclass
c) Defining multiple methods with the same name but different parameter lists in a class
d) Changing the return type of an existing method

Answer:

c) Defining multiple methods with the same name but different parameter lists in a class

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?

a) Method name
b) Return type
c) Number and type of parameters
d) Access modifier

Answer:

c) Number and type of parameters

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?

a) Yes
b) No
c) Only if the parameters are different
d) Only if the method is static

Answer:

a) Yes

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?

a) Yes
b) No
c) Only if the method is private
d) Only if the method is in an interface

Answer:

b) No

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?

a) Increased program speed
b) Improved code readability and reusability
c) Enhanced security
d) Reduced memory usage

Answer:

b) Improved code readability and reusability

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?

a) int add(int a, int b) and double add(double x, double y)
b) int add(int a, int b) and int add(int a)
c) void add(int a, int b) and int add(int a, int b)
d) Both a and b

Answer:

d) Both a and b

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?

a) Yes
b) No
c) Only in abstract classes
d) Only if the class is final

Answer:

a) Yes

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?

a) The program will not compile
b) The program will compile, and the return type will be decided at runtime
c) The program will compile, but an exception will occur at runtime
d) The program will compile and execute without issues

Answer:

a) The program will not compile

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?

a) By allowing runtime method binding
b) By enabling compile-time method selection
c) By providing inheritance mechanisms
d) By enabling dynamic method dispatch

Answer:

b) By enabling compile-time method selection

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?

a) Yes
b) No
c) Only if parameters are of different types
d) Only if parameters are of the same type

Answer:

a) Yes

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?

a) Due to inheritance
b) It violates encapsulation
c) Method overloading is class-specific
d) Due to polymorphism

Answer:

c) Method overloading is class-specific

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?

a) To call an overloaded method
b) To refer to the current class instance variables
c) To distinguish between local and instance variables
d) Both a and c

Answer:

d) Both a and c

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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top