Given: 11. public class Yikes { 12. 13. public static void go(Long n) {System.out.println("Long ");} 14. public static void go(Short n) {System.out.println("Short ");} 15. public static void go(int n) {System.out.println("int ");} 16. public static void main(String [] args) { 17. short y = 6; 18. long z = 7; 19. go(y); 20. go(z); 21. } 22. } What is the result?

  1. int Long

  2. Short Long

  3. Compilation fails

  4. An Exception thrown at runtime


Correct Option: A

AI Explanation

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

Option A) int Long - This option is correct.

In the given code, the go() method is overloaded with three different parameter types: Long, Short, and int.

On line 19, the go(y) statement passes a short variable y as an argument to the go() method. Since there is no method that exactly matches the parameter type short, the compiler looks for the closest matching method. In this case, it finds the go(int n) method, which can accept a short argument due to implicit narrowing conversion. Therefore, "int " is printed.

On line 20, the go(z) statement passes a long variable z as an argument to the go() method. Since there is an exact match with the parameter type Long, the go(Long n) method is called and "Long " is printed.

Therefore, the output will be "int Long".

Option B) Short Long - This option is incorrect because the go(Short n) method is not called in the given code.

Option C) Compilation fails - This option is incorrect because there are no compilation errors in the given code.

Option D) An Exception thrown at runtime - This option is incorrect because there are no exceptions thrown in the given code.

The correct answer is option A.

Find more quizzes: