Multiple choice general knowledge science & technology

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); } }

  1. The program runs and prints 2 once

  2. The program runs and prints 2 twice

  3. The program has a syntax error because the second m method is defined, but not invoked in the main method

  4. The program has a syntax error because the two methods m have the same signature

Reveal answer Fill a bubble to check yourself
D Correct answer
Explanation

Methods are distinguished by their signature - the method name AND parameter list (types and order). Both m() methods have the same signature (m(int)) - only the return type differs, which is not sufficient for overloading. This causes a compilation error.