📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    The following Java code attempts to demonstrate method

    overloading, but it has a compilation error. How should it be corrected? public class Calculator {     public int add(int a, int b) {         return a + b;     }     public double add(int x, int y) { // Problematic         return x + y;     }     public static void main(String[] args) {         Calculator calc = new Calculator();         System.out.println(calc.add(5, 10));     } }
    A Change the return type of the second `add` method to `int`. Correct Answer Incorrect Answer
    B Change the parameter types of the second `add` method (e.g., `double x, double y`). Correct Answer Incorrect Answer
    C Remove the `public` keyword from the second `add` method. Correct Answer Incorrect Answer
    D Rename the second `add` method. Correct Answer Incorrect Answer
    E Add `@Override` annotation to the second `add` method. Correct Answer Incorrect Answer

    Solution

    The correct answer is B

    Practice Next
    ask-question