1Z0-803 · Question #259
Given: class Base { public static void main(String[] args) { System.out.println("Base " + args[2]); } } public class Sub extends Base{ public static void main(String[] args) { System.out.println("Over
The correct answer is B. Overridden 20. When executing a Java program via java ClassName, the JVM directly calls the main method of the specified ClassName. Static methods, including main, cannot be overridden in the same way instance methods are.
Question
Given:
class Base { public static void main(String[] args) { System.out.println("Base " + args[2]); } } public class Sub extends Base{ public static void main(String[] args) { System.out.println("Overriden " + args[1]); } } And the commands:
javac Sub.java java Sub 10 20 30 What is the result?
Options
- ABase 30
- BOverridden 20
- COverridden 20
- DBase 30
How the community answered
(27 responses)- A19% (5)
- B70% (19)
- C4% (1)
- D7% (2)
Why each option
When executing a Java program via `java ClassName`, the JVM directly calls the `main` method of the specified `ClassName`. Static methods, including `main`, cannot be overridden in the same way instance methods are.
Incorrect, `Base.main` is not executed because `java Sub` explicitly targets `Sub.main`.
B is correct because the `java Sub` command directly invokes the `main` method defined in the `Sub` class, not the `main` method of its `Base` class. The arguments "10", "20", "30" are passed to `Sub.main`, and `args[1]` refers to "20", resulting in the output "Overridden 20".
Incorrect, this choice is identical to the correct answer and represents the correct output.
Incorrect, `Base.main` is not executed because `java Sub` explicitly targets `Sub.main`.
Concept tested: Java program execution, static method overriding, command-line arguments.
Source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-8.html#jls-8.4.8.2
Topics
Community Discussion
No community discussion yet for this question.