Multiple choice technology architecture

public class Dec26 { public static void main(String[] args) { short a1 = 6; new Dec26().go(a1); new Dec26().go(new Integer(7)); } void go(Short x) { System.out.print("S "); } void go(Long x) { System.out.print("L "); } void go(int x) { System.out.print("i "); } void go(Number n) { System.out.print("N "); } } What is the result?

  1. i L

  2. i N

  3. S L

  4. S N

  5. Compilation fails.

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

For go(a1): short primitive widens to int, calling go(int) and printing 'i'. For go(new Integer(7)): Integer is an Object, not a primitive. It doesn't match go(Long) (Integer != Long), doesn't match go(Short) (Integer != Short), so it matches go(Number) because Integer IS-A Number. Output is 'i N'. This tests method overloading resolution with autoboxing and widening reference conversions.

AI explanation

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

Option A) i L - This option is incorrect because the argument passed to the go method in the line new Dec26().go(a1); is of type short, and there is no method with a short parameter. The closest matching method is void go(int x).

Option B) i N - This option is correct. The argument passed to the go method in the line new Dec26().go(a1); is of type short, and there is no method with a short parameter. However, there is a method with an int parameter, so that method is invoked. The argument passed to the go method in the line new Dec26().go(new Integer(7)); is of type Integer, which is a subclass of Number. The closest matching method for Number is void go(Number n). Therefore, the output will be "i N".

Option C) S L - This option is incorrect because there is no method with a short parameter. The argument passed to the go method in the line new Dec26().go(a1); is of type short, but there is no method with a short parameter. The closest matching method is void go(int x). The argument passed to the go method in the line new Dec26().go(new Integer(7)); is of type Integer, which is a subclass of Number. The closest matching method for Number is void go(Number n). Therefore, the output will be "i N".

Option D) S N - This option is incorrect because the argument passed to the go method in the line new Dec26().go(a1); is of type short, but there is no method with a short parameter. The closest matching method is void go(int x). The argument passed to the go method in the line new Dec26().go(new Integer(7)); is of type Integer, which is a subclass of Number. The closest matching method for Number is void go(Number n). Therefore, the output will be "i N".

Option E) Compilation fails - This option is incorrect because the code will compile successfully. Although there is an overload resolution ambiguity between the void go(int x) and void go(Number n) methods for the argument of type short, the closest matching method is chosen at runtime.

The correct answer is Option B) i N. This option is correct because the argument passed to the go method in the line new Dec26().go(a1); is of type short, which is implicitly widened to int (the closest matching method). The argument passed to the go method in the line new Dec26().go(new Integer(7)); is of type Integer, which is a subclass of Number (the closest matching method). Therefore, the output will be "i N".