1Z0-809 · Question #150
Given: MainTest.java: public class MainTest { public static void main(int[] args) { System.out.println("int main " + args[0]); } public static void main(Object[] args) { System.out.println("Object…
The correct answer is B. String main 1. Java's JVM entry point is hardcoded to look for exactly public static void main(String[] args) - this is the only signature the runtime recognizes as a program's starting point. When you pass 1 2 3 on the command line, the JVM bundles them as String[], calls the matching…
Question
Options
- AAn exception is thrown at runtime.
- BString main 1
- CObject main 1
- DInt main 1
- ECompilation fails.
How the community answered
(55 responses)- A5% (3)
- B82% (45)
- C9% (5)
- D2% (1)
- E2% (1)
Explanation
Java's JVM entry point is hardcoded to look for exactly public static void main(String[] args) - this is the only signature the runtime recognizes as a program's starting point. When you pass 1 2 3 on the command line, the JVM bundles them as String[], calls the matching overload, and args[0] is the string "1", producing String main 1.
Why the distractors fail:
- A - No exception occurs; the code runs cleanly with three valid arguments.
- C - The JVM never selects
main(Object[])as an entry point, even thoughString[]is technically a subtype ofObject[]; method selection for the entry point is not based on polymorphism. - D - Command-line arguments are always
String[], never primitives, andmain(int[])is simply ignored by the JVM as an entry point. - E - Overloading
mainwith different signatures is perfectly legal Java; it compiles without issue.
Memory tip: Think of it as a contract - the JVM signed a contract that says "I will only call main(String[] args)." You can add as many overloaded main variants as you like, but the JVM will walk past all of them to find its one specific signature.
Community Discussion
No community discussion yet for this question.