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)
A person invests ₹50,000 at 12% p.a. compounded annually. Find the compound value after 3 years.
A company reports Net Sales of ₹100 lakh, Gross Profit of ₹40 lakh, and Operating Profit of ₹25 lakh. Interest is ₹5 lakh and Tax is ₹4 lakh. ...
Calculate the Debt Equity ratio of the company?
ABC Ltd. records a lease as an asset and a corresponding liability in its books under Ind AS 116. The company uses this asset in operations and deprecia...
For the financial year ended 31st March 2023, the figures extracted from the balance sheet of ABC Ltd. are as under:
Opening stock 29,000 Closing...
Under the provisions of the SARFAESI Act, 2002, specialized entities called ARCs are regulated by RBI. What does the term ARC stand for?
Which of the following insurance contracts is likely to have a significant insurance risk component?
Which of the following financial statements shows a company's retained earnings over time?
Which of the following is not an accounting equation?
If nothing is given in the financial statements about the three accounting assumptions, then it is to be treated as it. Â