26.8k views
4 votes
Analyze the following code

public class Test {
public static void main(String[] args) {
System.out.println(m(2));
}
public static int m(int num) {
return num;
}
public static void m(int num) {
System.out.println(num);
}
}
(a) The program has a syntax error because the two methods m have the same signature
(b) The program has a syntax error because the second m method is defined, but not invoked in the main method
(c) The program runs and prints 2 once
(d) The program runs and prints 2 twice
(e) The program runs and prints 2 thrice.

User Lgwest
by
7.9k points

1 Answer

5 votes

Final answer:

The code has a compile-time error due to two methods named 'm' with the same signature, causing them to be indistinguishable from the compiler.

Step-by-step explanation:

The given code contains two methods with the name m but with different return types. In Java, overloading methods must have different parameter lists. Since both methods m have the same parameter list (a single int parameter), the code will result in a compile-time error. The correct answer is (a) The program has a syntax error because the two methods m have the same signature.

User Vikash Kumar Verma
by
8.2k points