Bird
0
0

What will be the output when the following Java program is executed?

medium📝 Predict Output Q5 of 15
Java - Methods and Code Reusability

What will be the output when the following Java program is executed?

class Calculator {
  int multiply(int a, int b) { return a * b; }
  double multiply(double a, double b) { return a * b; }
}
public class Test {
  public static void main(String[] args) {
    Calculator calc = new Calculator();
    System.out.println(calc.multiply(4, 5));
    System.out.println(calc.multiply(3.0, 2));
  }
}
ACompilation error
B20 Compilation error
C20 6
D20 6.0
Step-by-Step Solution
Solution:
  1. Step 1: Analyze method calls

    calc.multiply(4, 5) calls multiply(int, int) returning 20.
  2. Step 2: Analyze second call

    calc.multiply(3.0, 2) matches multiply(double, double) because 2 is promoted to double, returning 6.0.
  3. Final Answer:

    20 6.0 -> Option D
  4. Quick Check:

    Primitive widening allows int to double [OK]
Quick Trick: int can widen to double in overloaded methods [OK]
Common Mistakes:
  • Expecting int multiply for mixed double and int arguments
  • Assuming compilation error due to mixed types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes