Method Overloading in Java Tutorial and Interview MCQ
Table of Contents
Study and learn Interview MCQ Questions and Answers on Java Method Overloading. Attend job interviews easily with these Multiple Choice Questions
what is method overloading in java
Java Method Overloading is nothing but the ability to write more than one Method with the same name in a Class with varying Return-Types or Parameters. In other words, you should maintain unique method signatures for all methods with the same name to successfully Overload. Let us know more about Method Overloading in this Java Tutorial.
You can read more about Constructor Overloading in Java for better understanding.
Method Overloading in Java
Java methods contain reusable pieces of code that can be called any number of times. and Java methods can receive arguments and return values. Parameters or Return values can be primitives or objects.
We simply maintain unique method signatures while maintaining the same method name to Overload a Java Method.
Rules to Overload a method:
- If Parameter-list is the same, Return-type can be Super-Class or Sub-Class type.
- and If the Parameter list is different, the return value can be either the same or different.
- If the return-type is same, the Parameter or Argument list should be distinct or different.
Method Overloading in Java example
public class Bank { int amount=0; void addAmount(int amt) { amount = amount + amt; } void addAmount(String strAmt) { amount = amount + Integer.parseInt(strAmt); } int addAmount(double amt) { amount = amount + (int)amt; return amount; } }
In the above example, we have Overloaded a Java method namely “addAmount“. We have used “void” and “int” as return types for all the methods with the same name “addAmount”. and We differentiated the overloaded methods by varying the data-type of the received parameter or argument. We have shown variation by using int, String and double types. The String is an Object-type parameter.
Method Overloading in Java example 2.
class School { int message(String msg) //error overloading { System.out.println("Message: " + msg); return 0; //some value } void message(String msg) //eror overloading { System.out.println("Message: " + msg); } }
Above example throws a Java error like “Duplicate method message(String) in type School“. Because the two methods have the same parameter list and different return-types. Return types should have been Subclass and Superclass types to successfully Overload methods.
[WpProQuiz 160]
Method Overloading in Java Interview MCQ
1) To successfully overload a method in Java, the return types must be ___.
A) Same
B) Different
C) Same but using superclass or subclass types also work
D) None
Answer [=] C
2) To successfully overload a method in Java, the argument-list or parameter-list must be ___.
A) Same
B) Different
Answer [=] B
3) What is the output of the below Java program with multiple methods?
public class MethodOverloading1 { void show(int a, char b) { System.out.println("KING KONG"); } void show(char a, int b) { System.out.println("JIM JAM"); } public static void main(String[] args) { MethodOverloading1 m = new MethodOverloading1(); m.show(10, 'A'); m.show('B', 10); } }
A)
JIM JAM JIM JAM
B)
KING KONG KING KONG
C)
KING KONG JIM JAM
D) compiler error
Answer [=] C
Explanation:
Method signatures are clearly different. So both methods work.
4) To successfully overload a method in Java, the method names must be ___.
A) Same
B) Different
C) Same or different
D) None
Answer [=] A
5) What is the output of the below Java program?
public class MethodOverloading2 { int info() { System.out.println("PLANE"); return 0; } void info() { System.out.println("AIRPORT"); } public static void main(String[] args) { MethodOverloading2 m = new MethodOverloading2(); int a = m.info(); } }
A) PLANE
B) AIRPORT
C) Compiler error
D) None
Answer [=] C
Explanation:
Both methods with the same name “info” do not overload successfully as the return types are different (void and int).
6) What is the output of the below Java program with method overloading?
class Wood{ } class SubWood extends Wood{ } public class MethodOverloading3 { Wood display(int a) { System.out.println("PINE"); return new Wood(); } SubWood display() { System.out.println("TEAK"); return new SubWood(); } public static void main(String[] args) { MethodOverloading3 m = new MethodOverloading3(); m.display(); } }
A) PINE
B) TEAK
C) Compiler error
D) None
Answer [=] B
Explanation:
Return types for the method display() are Wood and SubWood. As these types are of superclass-subclass, it is a valid method overloading.
7) What is the output of the below Java program trying to overload a method “jump”?
class Rabbit{ } class WildRabbit extends Rabbit{ } public class MethodOverloading4 { Rabbit jump() { System.out.println("Rabbit Jump"); return new Rabbit(); } WildRabbit jump() { System.out.println("Wild Rabbit Jump"); return new WildRabbit(); } public static void main(String[] args) { MethodOverloading4 obj = new MethodOverloading4(); obj.jump(); } }
A) Rabbit Jump
B) Wild Rabbit Jump
C) Compiler error
D) None
Answer [=] C
Explanation:
The above program does not overload the method jump() properly as the argument list is not distinct. So the compiler reports “duplicate method error”.
8) Java method overloading implements the OOPS concept ___.
A) Inheritance
B) Polymorphism
C) Encapsulation
D) None
Answer [=] B
9) Which is the overloaded static method of Math class to get absolute value in Java?
A) Math.abs(int)
B) Math.abs(float)
C) Math.abs(double)
D) All the above
Answer [=] D
Explanation:
Notice that static methods can be overloaded in Java without any compiler error. So, Math.abs() works out of the box with different type arguments.
10) What is the output of the below Java program that tries to overload a method “abs”?
public class MyAbs { static int abs(int a) { return a<0?-a:a; } static float abs(float b) { return b<0?-b:b; } static double abs(double c) { return c<0?-c:c; } public static void main(String[] args) { int a=-10; float b=-4.56f; double c=-10.123; System.out.println(MyAbs.abs(a) + ", " + MyAbs.abs(b) + ", " + MyAbs.abs(c)); } }
A) No output
B) Compiler error
C) 10, 4.56, 10.123
D) -10, -4.56, -10.123
Answer [=] C
Explanation:
The above program demonstrates the usage of Overloaded methods. “abs” function returns the absolute value of the input.