You have the following code in a file called Test.java
class Base{
public static void main(String[] args){
System.out.println("Hello");
}
}
public class Test extends Base{}
What will happen if you try to compile and run this?
class Base{
public static void main(String[] args){
System.out.println("Hello");
}
}
public class Test extends Base{}
A. It will fail to compile.
B. Runtime error
C. Compiles and runs with no output.
D. Compiles and runs printing
Answer: Option D
Solution(By Examveda Team)
This will compile and print "Hello".
The entry point for a standalone java program is the main method of the class that is being run. The java run-time system will look for that method in class Test and find that it should have such a method. It does not matter whether it is defined in the class itself or is inherited from a parent class.
Join The Discussion
Comments ( 1 )
What is the default access modifier for class members in Java if no access modifier is specified?
A. public
B. protected
C. private
D. package-private
A. private
B. public
C. protected
D. package-private
What keyword is used to declare a constant in Java, which is implicitly final and static?
A. final
B. const
C. static
D. constant
A. protected
B. private
C. public
D. default
I tried and run the same program in notepad(Command Prompt) and eclipse IDE as well..But in eclipse it compiled but did not run and in command prompt it shows error.. This might be because ,Java file is created of Test which is public and public static void main is placed in Base class which is default.
PSVM can be placed only in public method..
I m skeptical about my knowledge..Can anyone come up with solution ??