Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

public class test { public static void main(string args) { system.out.p…

Question

public class test {
public static void main(string args) {
system.out.println(xmethod(5, 500l));
}

public static int xmethod(int n, long l) {
system.out.println(\int, long\);
return n;
}

public static long xmethod(long n, long l) {
system.out.println(\long, long\);
return n;
}
}
a the program displays long, long followed by 5.
b the program displays int, long followed by 5.
c the program does not compile because the compiler cannot distinguish which xmethod to invoke.
d the program runs fine but displays things other than 5.

Explanation:

Step1: Analyze method overloading

In Java, method overloading is based on parameter types. Here, when calling xMethod(5, 500L), the first argument 5 is of type int and the second 500L is of type long.

Step2: Match method signature

The xMethod(int n, long l) method has a signature that exactly matches the argument types passed (int and long). So this method will be invoked.

Answer:

B. The program displays int, long followed by 5.