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)
निम्नलिखित वाक्य में सही मुहावरे छाँटिए
पढ़ाई में मेह...
अनुप्रास अलंकार का कौन - सा उदाहरण है ?
निम्न में ‘ आदि’ शब्द का विलोम क्या है?
कौन.सी ध्वनि अल्पप्राण है?
'स्वागत' शब्द में कौन सा उपसर्ग है ?
कष्ट से संपन्न होने वाला -
'ए लड़की' किसकी रचना है :
दिए गए शब्द के पर्याय के लिए चार चार विकल्प दिए गए हैं। उचि�...
निम्नलिखित में से “अखंड” का पर्यायवाची शब्द क्या है?
निम्नलिखित शब्द का सही विलोम छाँटिए -
चिरंतन