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)
'द्रौपदी' का पर्यायवाची नहीं है
सूची- I को सूची – II में सुमेलित कीजिए और सूचियों के नीचे दिए �...
राजभाषा में कुल कितने नियम है ?
निम्नलिखित वाक्यों के प्रयोग पर विचार कीजिये -
1.पूज्यन�...
राम राधा के लिए फल लाया हैं कौन सा कारक है
प्रश्नवाचक तथा विस्मयादिबोधक को छोड़कर सभी वाक्यों के अंत...
जिसके बारे में कुछ बताया जाता है, उसे क्या कहा जाता है।
‘बालक चाँद-सा सुंदर है’ में कौन-सा अलंकार है?
दिए गए शब्द के लिए चार विकल्प दिए गए हैं उच्च विकल्प चु�...
अशुद्ध वर्तनी वाला शब्द है __________