Click the Exhibit button. 1. public class A { 2. public String doit(int x, int y) { 3. return “a”; 4. } 5. 6. public String doit(int... vals) { 7. return “b”; 8. } 9. } Given: 25. A a=new A(); 26. System.out.println(a.doit(4, 5)); What is the result?

  1. Line 26 prints “a” to System.out

  2. Line 26 prints ‘b” to System.out

  3. An exception is thrown at line 26 at runtime.

  4. Compilation of class A will fail due to an error in line 6


Correct Option: A
Explanation:

To solve this question, the user should understand the concept of method overloading and how Java resolves the method call based on the number and type of arguments passed.

In the given code, class A has two methods named doit. One method takes two integer arguments, and the other takes an arbitrary number of integer arguments.

When we call a.doit(4,5) on line 26, Java examines both methods in class A that are named doit and checks to see which one matches the argument types. Since there is a doit method with two integer parameters, Java selects that method, and the String "a" is returned by that method.

Therefore, the output of the given code is:

The Answer is: A. Line 26 prints “a” to System.out.

Find more quizzes: