What is method overloading in Java?
A
Creating methods with the same name but different return types
B
Creating methods with different names
C
Defining multiple methods with the same name but different parameter lists
D
Using a method inside another method
Analysis & Theory
Method overloading means creating multiple methods with the same name but different parameter types, numbers, or orders.
Which of the following is a valid overloaded method pair?
A
void sum(int a), int sum(int a)
B
int sum(int a, int b), int sum(int a, int b)
C
void sum(int a), void sum(int b)
D
int sum(int a), int sum(int a, int b)
Analysis & Theory
The two `sum` methods have different parameter counts, so they are valid overloads.
Can overloaded methods have different return types only?
B
No, parameters must differ
C
Yes, if both are static
Analysis & Theory
Overloading requires a difference in parameter list — return type alone is not enough.
What is the output?
```java
public static void show(int a) {
System.out.println("int");
}
public static void show(String a) {
System.out.println("String");
}
show(5);```
Analysis & Theory
`show(5)` matches the method with `int` parameter.
Which of these methods are overloaded?
```java
void display(int a)
void display(float a)
void display(int a, int b)```
Analysis & Theory
All methods have the same name but different parameter types/counts.
Can method overloading occur within the same class?
C
Only with static methods
D
Only in abstract classes
Analysis & Theory
Overloading happens in the same class where multiple methods share the same name.
Is the following valid?
```java
public void test(int a) {}
public int test(int a) { return a; }```
B
No, return type alone is not enough
C
Yes, because they return differently
D
Yes, if both are public
Analysis & Theory
This causes a compile error because overloading must differ in parameters, not return types alone.
Which call will be matched?
```java
void print(String s)
void print(Object o)
...
print("Hello");```
Analysis & Theory
The most specific method (`String`) will be matched over `Object`.
What is the benefit of method overloading?
C
Improved readability and reusability
D
Allows runtime polymorphism
Analysis & Theory
Overloading improves readability and allows reusing method names for similar operations.
What is the output?
```java
void print(int a)
void print(int a, int b)
...
print(10);```
C
The method with one parameter
D
The method with two parameters
Analysis & Theory
`print(10)` matches the method with one `int` parameter.