nerdexam
Oracle

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

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 main " + args[0]); } public static void main(String[] args) { System.out.println("String main " + args[0]); } } and commands: javac MainTest.java java MainTest 1 2 3 What is the result?

Options

  • AAn exception is thrown at runtime.
  • BString main 1
  • CObject main 1
  • DInt main 1
  • ECompilation fails.

How the community answered

(55 responses)
  • A
    5% (3)
  • B
    82% (45)
  • C
    9% (5)
  • D
    2% (1)
  • E
    2% (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 though String[] is technically a subtype of Object[]; method selection for the entry point is not based on polymorphism.
  • D - Command-line arguments are always String[], never primitives, and main(int[]) is simply ignored by the JVM as an entry point.
  • E - Overloading main with 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.

Full 1Z0-809 Practice