nerdexam
Oracle

1Z0-808 · Question #15

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 C. String main 1. Option C is correct because the JVM always uses public static void main(String[] args) as the program entry point - this is a fixed contract, not subject to overload resolution. Command-line arguments (1 2 3) are always passed as String[], making args[0] the String "1", so the…

Working with Methods and Encapsulation

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

  • Aint main 1
  • BObject main 1
  • CString main 1
  • DCompilation fails
  • EAn exception is thrown at runtime

How the community answered

(47 responses)
  • A
    2% (1)
  • B
    11% (5)
  • C
    81% (38)
  • D
    2% (1)
  • E
    4% (2)

Explanation

Option C is correct because the JVM always uses public static void main(String[] args) as the program entry point - this is a fixed contract, not subject to overload resolution. Command-line arguments (1 2 3) are always passed as String[], making args[0] the String "1", so the output is String main 1.

Why the distractors are wrong:

  • A - main(int[]) is never called by the JVM as an entry point, and command-line args cannot be passed as primitives anyway.
  • B - main(Object[]) is also ignored by the JVM at startup; String[] is more specific and is the designated signature.
  • D - All three methods are syntactically valid Java; the code compiles without error.
  • E - No exception occurs; args[0] is "1" (a valid non-null String), so no ArrayIndexOutOfBoundsException is thrown.

Memory tip: Think of main(String[] args) as a hardcoded contract with the JVM - it won't "pick the best overload," it only looks for that exact signature. Any other main variant is just a regular method nobody automatically calls.

Topics

#Method overloading#main method#JVM entry point#Method resolution

Community Discussion

No community discussion yet for this question.

Full 1Z0-808 Practice