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)
“Jurisdiction” का सही हिंदी अर्थ क्या है ?
निम्नलिखित का सही अनुवाद चुनें:
(i) Export documents – निर्यात दस्त�...
राजभाषा नियम , 1976 के अंतर्गत भारत को पत्राचार की दृष्टि से क...
नीचे दिए गए वाक्यों के सही अनुवाद का मिलान करें और उचित वि�...
केंद्र सरकार का कौन-से मंत्रालय का विभाग राजभाषा नीति ...
‘ पउमचरिउ ’ किसकी रचना है ?
निम्नलिखित हिंदी वाक्य का अंग्रेजी अनुवाद वाला सही विकल�...
Select the correct Hindi translation of the words given below:
समानता , स्वतंत्रता , बंधुत्व , न...
निम्न में से कौन सी भाषा भारतीय संविधान की आठवीं अनुसूची म...
निम्नलिखित का सही अनुवाद चुनें:
(i) Benchmark interest rate – मानक लाभ �...