Question
What will the following Java code snippet output when
executed, which uses a simple constructor and method overloading ? class Calculator { Â Â Â int add ( int a, int b) { Â Â Â Â Â Â Â return a + b; Â Â Â } Â Â Â double add ( double a, double b) { Â Â Â Â Â Â Â return a + b; Â Â Â } Â Â Â public static void main (String[] args) { Â Â Â Â Â Â Â Calculator calc = new Calculator (); Â Â Â Â Â Â Â System.out.println(calc.add( 5 , 10 ));Â Â Â Â Â // Line 1 Â Â Â Â Â Â Â System.out.println(calc.add( 5.5 , 10.5 ));Â // Line 2 Â Â Â }}Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ÂSolution
In this example, the Calculator class defines two add() methods with the same name but different parameter types: one for integers and another for doubles. This is an example of method overloading . Let's examine why A is the correct answer and the other options are incorrect:
- Explanation of Correct Option (A):
- The method add(int a, int b) accepts integers and returns an integer result. When calc.add(5, 10)
Select the option that is related to the third number in the same way as the second number is related to first number and the sixth number is related t...
Select the number -pair from the given options that shares a similar relationship as the given number-pair.
192-108
Select the option that is related to fifth letter cluster in the same way as the second - letter cluster is related to the first letter- cluster and the...
Select the option that is related to the third number in the same way as the second number is related to the first number and the sixth number is relat...
If all the digits in each of the following numbers are arranged in descending order within the numbers, then which of the following will be highest numb...
Select the option in which the numbers are related in the same way as are the numbers in the given set.
(15, 48, 98)
Select the correct option that indicates the arrangement of the following words in a logical and meaningful order.
1. Nice
2. Europe
<...Select a suitable figure from the answer figures that would replace the question mark (?) from question figure.
Which of the following is related to the third word as the second is related to the first.
Light:Darkness::Knowledge:?
Given set:Â [20, 82, 31]