šŸ“¢ Too many exams? Don’t know which one suits you best? Book Your Free Expert šŸ‘‰ call Now!

  • google app store apple app store
  • āœ–

      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
      More IT Operating System Questions
      ask-question