Multiple choice java

What is the output?

public class Wow {
 public static void go(short n) {System.out.println("short");}
 public static void go(Short n) {System.out.println("SHORT");}
 public static void go(Long n) {System.out.println(" LONG");}
 public static void main(String [] args) {
   Short y = 6;
   int z = 7;
   go(y);
   go(z);
 }
}

  1. short LONG

  2. SHORT LONG

  3. Compilation fails.

  4. An exception is thrown at runtime.

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

The method call go(z) fails because int cannot be autoboxed to Short, widened to Long, and there is no go(int) method. While go(y) works (autoboxing to Short), the presence of the invalid call go(z) causes a compilation error overall.

AI explanation

To answer this question, let's go through each option to understand why it is correct or incorrect:

Option A) short LONG - This option is incorrect because the expected output is "Compilation fails" (as mentioned in the correct answer). Option B) SHORT LONG - This option is incorrect because the expected output is "Compilation fails" (as mentioned in the correct answer). Option C) Compilation fails - This option is correct. The reason for this is that there are multiple overloaded methods with similar parameter types (short, Short, and Long). When the method is called with the variable 'y', which is of type Short, it is ambiguous whether it should call the method go(short n) or go(Short n) because both are valid options. This ambiguity results in a compilation error. Option D) An exception is thrown at runtime - This option is incorrect because the issue here is a compilation error, not a runtime exception.

The correct answer is C. Compilation fails.

Please let me know if I can help you with anything else.