Java Method Overloading MCQ Questions And Answers
Q. What is method overloading in Java?
A. Having two methods with the same name but different return typesB. Having multiple methods within a class with the same name but different parameters
C. Having methods that can override each other
D. Having only one method in a class
Q. Can method overloading be achieved by changing the return type of the method?
A. Yes, method overloading can be achieved by changing the return type of the methodB. No, method overloading cannot be achieved by changing the return type of the method
C. It depends on the Java version being used
D. None of the above
Q. Is it required for overloaded methods to have the same return type?
A. Yes, overloaded methods must have the same return typeB. No, overloaded methods can have different return types
C. It depends on the Java version being used
D. None of the above
Q. Can method overloading be achieved by changing the access modifiers of the method?
A. Yes, method overloading can be achieved by changing access modifiersB. No, method overloading cannot be achieved by changing access modifiers
C. It depends on the Java version being used
D. None of the above
Q. Can method overloading be achieved by changing the exception thrown by the method?
A. Yes, method overloading can be achieved by changing exceptions thrownB. No, method overloading cannot be achieved by changing exceptions thrown
C. It depends on the Java version being used
D. None of the above
Q. What happens if two methods have the same name and parameters but different return types?
A. The compiler throws an errorB. The methods are considered overloaded
C. The second method is considered overriding the first method
D. The methods cannot have the same name
Q. Can method overloading be achieved by changing the order of parameters in the method?
A. Yes, method overloading can be achieved by changing the parameter orderB. No, method overloading cannot be achieved by changing the parameter order
C. It depends on the Java version being used
D. None of the above
Q. Can method overloading be achieved in the same class or in a subclass?
A. Method overloading can only be achieved in the same classB. Method overloading can only be achieved in a subclass
C. Method overloading can be achieved in both the same class and in a subclass
D. None of the above
Q. When is method overloading useful in Java programming?
A. When you want to have multiple methods with the same name and return typeB. When you want to perform different tasks based on the number of parameters
C. When you want to have methods that can override each other
D. When you want to have methods with the same name but different parameters
Q. Can methods with the same name and parameters but different return types be defined in the same class?
A. Yes, multiple methods with the same name and parameters but different return types can be defined in the same classB. No, methods with the same name and parameters must have the same return type in the same class
C. It depends on the access modifiers of the methods
D. None of the above
Q. What is Java method overloading?
A.// Method Overloading Example public class Calculator { public int add(int a, int b) { return a + b; } public double add(double a, double b) { return a + b; } }B.
// Method Overloading Example public class Calculator { public int add(int a, int b) { return a + b; } public int add(int a, int b, int c) { return a + b + c; } }C.
// Method Overloading Example public class Calculator { public int add(int a, int b) { return a + b; } public String add(String a, String b) { return a + b; } }D.
// Method Overloading Example public class Calculator { public double add(double a, double b) { return a + b; } public double add(double a, double b, double c) { return a + b + c; } }
Q. What is the significance of method overloading in Java?
A.// Method Overloading Example public class Printer { public void print(String message) { System.out.println(message); } public void print(int number) { System.out.println(number); } }B.
// Method Overloading Example public class Printer { public void print(String message) { System.out.println(message); } public void print(String message, int times) { for (int i = 0; i < times; i++) { System.out.println(message); } } }C.
// Method Overloading Example public class Printer { public void print(String message) { System.out.println(message); } public void print(String prefix, String message) { System.out.println(prefix + message); } }D.
// Method Overloading Example public class Printer { public void print(String message) { System.out.println(message); } public String print(String message) { return message; } }
Q. Which of the following is NOT a valid way to overload a method in Java?
A.// Method Overloading Example public class MathOperations { public int multiply(int a, int b) { return a * b; } public double multiply(int a, int b) { return a * b; } }B.
// Method Overloading Example public class MathOperations { public int add(int a, int b) { return a + b; } public int add(int a, int b, int c) { return a + b + c; } }C.
// Method Overloading Example public class MathOperations { public void subtract(int a, int b) { System.out.println(a - b); } public void subtract(double a, double b) { System.out.println(a - b); } }D.
// Method Overloading Example public class MathOperations { public int divide(int a, int b) { return a / b; } public int divide(int a, int b, int c) { return a / b / c; } }