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 cost price of 20 earphones equals the selling price of 16 earphones. The ratio of the cost price of an earphone to a pouch is 3:2. The sum of the se...
A manufacturer produces three types of products: X, Y, and Z. The production costs are ₹200, ₹350, and ₹500 per unit, respectively. In a month, th...
A shopkeeper purchased an article for Rs. ‘a’ and marked it 140% above its cost price and sold it after giving two successive discounts of 480 and 2...
A shirt is marked 20% above the cost price and sold after a discount of Rs.104 at Rs.400. Find the cost price of the shirt.
- A trader sold a second-hand laptop for Rs. 42,000 at a 16% loss. What should be the selling price if he wants to earn a 14% profit?
A shopkeeper sells an article at a loss of 25%. If he had sold it for Rs 240 more, he would have made a profit of 15%. Find the cost price of the article.
Cost price of an article is 25% less than its marked price of article. The item is then sold for Rs. 5,950, achieving a profit margin of (40/3)%. Calcul...
Cost price of an article is X. The article is marked up by Y% and sold while offering a discount of 25%. The profit earned is (Y + 20). When the same ar...
The cost price of two cameras 'C' and 'D' is Rs. 40,000 and Rs. 50,000 respectively. The shopkeeper marked up the prices of camera 'C' and 'D' by 60% an...
The cost prices of a bookshelf and a wardrobe are in the ratio 2:5 respectively. If the seller earned a profit of 10% on the bookshelf and an overall pr...