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     }}                             
    A 15, 16.0 Correct Answer Incorrect Answer
    B 15.0, 16.0 Correct Answer Incorrect Answer
    C 15, 16 Correct Answer Incorrect Answer
    D 10, 10 Correct Answer Incorrect Answer
    E Compilation error due to method overloading Correct Answer Incorrect Answer

    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)

    Practice Next