Refer to the exhibit. Two Java classes are defined as shown. What is the output when the Sub class is executed?
Exhibit
Refer to the exhibit.
public class Main {
public static void main(String[] args) {
System.out.println("Hello");
}
}
And the command:
$ javac Main.java
$ java Main
Output:
Hello
Now consider this class in another file:
public class Sub extends Main {
public static void main(String[] args) {
System.out.println("World");
}
}
Compiled and run:
$ java Sub
What is the output?Trap 1: HelloWorld
Only Sub's main method runs, not both.
Trap 2: Hello
Sub's main method does not invoke Main's main; it prints 'World'.
Trap 3: No output (compilation error)
Both classes compile and run without error.
- A
HelloWorld
Why wrong: Only Sub's main method runs, not both.
- B
World
Sub's main method is executed, printing 'World'.
- C
Hello
Why wrong: Sub's main method does not invoke Main's main; it prints 'World'.
- D
No output (compilation error)
Why wrong: Both classes compile and run without error.