![]() |
VOOZH | about |
Prerequisite - Overloading in Java
1) What is the output of the following program?
a) 1
b) 0
c) Runtime error
d) Compilation error
Ans. (d)
Explanation: For method overloading, methods must have different signatures. Return type of methods does not contribute towards different method signature, so the code above give compilation error. Both getdata 1 and getdata 2 only differ in return types and NOT signatures.
2) What is the output of the following program?
a) 0
b) 1
c) Compilation error
d) Runtime error
Ans. (c)
Explanation: Methods that throws different exceptions are not overloaded as their signature are still the same.
3) What is the output of the following program?
a) Compilation error
b) Runtime error
c) GFG
d) None of these
Ans. (c)
Explanation: Access modifiers associated with methods doesn't determine the criteria for overloading. The overloaded methods could also be declared as final or static without affecting the overloading criteria.
4) What is the output of the following program?
a) GFG
b) GeeksforGeeks
c) Compilation error
d) Runtime error
Ans. (b)
Explanation: The order of argument are an important parameter for determining method overloading. As the order of attributes are different, the methods are overloaded.
5) What is the output of the following program?
a) GFG
b) Compilation error
c) Runtime error
d) GeeksforGeeks
Ans. (a)
Explanation: The order of argument are an important parameter for determining method overloading.
6) What is the output of the following program?
a) GFG
b) Compilation error
c) Runtime error
d) GeeksforGeeks
Ans. (a)
Explanation: This one is really simple. Different types of arguments contribute towards method overloading as the signature of methods is changed with different type of attributes. Whichever method matches the set of arguments passed in the main function will be called. Here, the first argument passed is double, and hence GFG is printed.