In: Computer Science
Analyze the following code:
public class Test {
public static void main(String[] args) {
xMethod(5, 500L);
}
public static void xMethod(int n, long l) {
System.out.println("int, long");
}
public static void xMethod(long n, long l) {
System.out.println("long, long");
}
}
Group of answer choices
The program prints 2 lines: int, long long, long
The program contains a compile error because Java can't tell which method to invoke.
The program prints long, long
The program prints int, long
The Correct Answer is Choice 4 which is
The program prints int, long
Explaination :
The program prints 2 lines: int, long long, long
This option is not correct because we are calling xMethod by passing integer and long (5 , 500L) values so it will call xMethod(int n, long l) and this method print only 1 line that is int , long
-----------------------
The program contains a compile error because Java can't tell which method to invoke.
This is not correct because in java compiler automatically find out which method has been called based on argument passed in overloading methods .
--------------------------
The program prints long, long
This option is not correct because we are calling xMethod by passing integer and long (5 , 500L) values so it will call xMethod(int n, long l) and this method print only 1 line that is int , long . If we passed (5L,500L) then this method xMethod(long n, long l) should be called by compiler
---------------------
The program prints int, long
This option is correct because we are calling xMethod by passing integer and long (5 , 500L) values so it will call xMethod(int n, long l) and this method print only 1 line that is int , long
If answer is helpful , please upvote . If have any doubt , let me know :)