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 main objective of the Horticulture Mission for North East and Himalayan States (HMNEH)?
Wetlands are vital ecosystems which sustain biological diversity. The government will promote their unique conservation values through………………...
From the following select the incorrect statement regarding the clonal selection in plant breeding?
Fair and remunerative price (FRP) is the minimum price at which sugarcane is to be purchased by sugar mills from farmers. The current FRP for the sugar ...
In the long form of periodic table the elements are arranged in the increasing order of
The "c" in "cGMP" stands for:
The major chemical component of the maize kernel is starch. Carbohydrates percentage (approx.) in maize crop is:
A molecular marker which amplified by PCR and polymorphic by length is a/an
DDT mainly affects which system of insect body?Â
Match list I with list II