Multiple choice technology programming languages

What will happen if you try to compile and run this ? public class Test{ static{ print(10); } static void print(int x){ System.out.println(x); System.exit(0); } }

  1. . Compiler error.

  2. Will throw a NoSuchMethod error at runtime.

  3. It will compile and run printing out "10"

  4. It will run with no output.

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

The code has a static block that calls print(10), which should work since print is a static method. However, the issue is that the static block runs before the main method, and the code has no main method at all. When you try to run this class, the JVM looks for a main method and throws a NoSuchMethodError at runtime because main() is missing. The static block will execute (and would print 10 if main existed), but the program fails due to the missing entry point.