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)
What is the primary objective of the Unified Portal for Agricultural Statistics (UPAg Portal)?
As per the Union budget 2025-26, the FDI limit in the insurance sector has been raised from 74% to—
Mission Poshan 2.0 was launched in 2021 to address the challenges of malnutrition in children, adolescent girls, pregnant women and lactating mothers. ...
Which two organizations recently signed a Memorandum of Understanding (MoU) to accelerate the achievement of Sustainable Development Goals (SDGs)?
Which of the following Statements regarding Foreign Portfolio Investment is FALSE?
Which of the following committee recommended Formation of Inter State Council ?
Which mobile app developed for farmers to increase awareness about MSP rates, nearest procurement centers, payment tracking, best farm practices etc. in...
Which of the following is NOT a primary component of PMAY(U)?
Consider the following Statements.
Assertion (A): Each Country uses a different Poverty line to determine the level of Poverty in their own count...
What percent of the total population is engaged in agriculture and allied activities as per the census 2011?