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)
- The speed ratio of Renu and Meenu is 4:5, respectively. Renu travels a distance of 300 km from point 'A' to 'B', while Meenu covers the same distance but i...
- A traveler at four-fifths of his normal speed takes 20 minutes longer than his usual time to reach his destination. What is his usual travel time?
The distance between points X and Y is in the ratio of 16:9 compared to the distance between points Y and Z. A person P travels f...
- Find the total surface area of a sphere whose radius is 18 cm.
A man starts chasing a biker who is 600 metres ahead. The man runs at 5 m/s while the biker rides at 3 m/s. How much time will the man take to catch him?
A truck travels at a speed of 60 km/h from city P to city Q and returns to city P by the same route at a speed of 100 km/h. What is the average speed of...
‘A’ and ‘B’ started moving towards each other at same time with a speed of 27 km/hr and 15 km/hr, respectively. If the distance covered by ‘A�...
Aman goes from his home to his office at a speed of 30 m/s and returns at 50% more speed. If the distance between his home and office is 1,800 metres, t...
A circular track has a radius of 140 meters. A person starts running from a point on the track and runs 3 complete rounds. What is the total distance co...
A cyclist moves at a speed of 20 km/h and plans to complete his journey in 3 hours. After finishing half the journey, he takes a break of 30 minutes. At...