Multiple choice technology programming languages

Analyze the following code. public class Test { public static void main(String[] args) { System.out.println(max(1, 2)); } public static double max(int num1, double num2) { System.out.println("max(int, double) is invoked"); if (num1 > num2) return num1; else return num2; } public static double max(double num1, int num2) { System.out.println("max(double, int) is invoked"); if (num1 > num2) return num1; else return num2; } }

  1. The program cannot compile because you cannot have the print statement in a non-void method

  2. The program cannot compile because the compiler cannot determine which max method should be invoked

  3. The program runs and prints 2 followed by "max(int, double)" is invoked.

  4. The program runs and prints 2 followed by "max(double, int)" is invoked.

Reveal answer Fill a bubble to check yourself
B Correct answer
Explanation

The call max(1, 2) passes two int literals. Java's overload resolution requires finding the most specific method. The two overloaded methods are max(int, double) and max(double, int). To call either, one int must be promoted to double. Both conversions are equally applicable, creating ambiguity. The compiler cannot determine which method to invoke, so compilation fails.