Examveda
Examveda

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?

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.


This Question Belongs to Java Program >> Declaration And Access Control

Join The Discussion

Comments ( 1 )

  1. Bhardwaj Joshi
    Bhardwaj Joshi :
    7 years ago

    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 ??

Related Questions on Declaration and Access Control