Interview Questions & Answers

Java

No, We cannot define a class without the main() method starting from Java 7. In the previous release of Java, we can have static Initializers as an alternative.

Example:-

JDK6

public class Main {
    static {
        System.out.println("Static Intializer");
        System.exit(0);
    }
}
output: Static Intializer

JDK7
output:

Error: Main method not found in class Main, please define the main method as: public static void main(String () args) 
or a JavaFX application class must extend javafx.application. Application

No, an argument of the main() method must be a String array. But, from the introduction of var args, you can pass var args of string type as an argument to the main() method. Again, var args are nothing but the arrays.

Example:-

public class Main {
    private void main(String... args) {

    }
}

No, the main() method must be declared as static so that JVM can call the main() method without instantiating its class. If you remove ‘static’ from the main() method signature, the compilation will be successful but the program fails at runtime.

Example:-

public class Main {
    private void main(String[] args) {
        System.out.println("main(String[] args)");

    }
}


output:
Error: Main method is not static in class com.java.ex. Main, please define the main method as: public static void main(String[] args)

No, the main() method must be public. You can’t define the main () method as private or protected or with no access modifier. This is because to make the main() method accessible to JVM.

Example:-

public class Main {
    private static void main(String[] args) {
        System.out.println("main(String[] args)");

    }
}

output:
Error: Main method not found in class com.java.ex. Main, please define the main method as: public static void main(String() args) 
or a JavaFX application class must extend javafx.application.Application

Yes, We can overload the main() method. A Java class can have any number of main() methods. But to run the java class, the class should have a main() method with signature as public static void main(String[] args).

Example:-

public class Main {
    public static void main(String[] args){
        System.out.println("main(String[] args)");
    }
    public static void main(int[] args){
        System.out.println("main(int[] args)");
    }
    public static void main(long[] args){
        System.out.println("main(long[] args)");
    }
}

output:
main(String[] args)

The main() method is static in Java, so the JVM can directly invoke it without instantiating the class’s object.

If the main() method is non-static, then JVM needs to create an instance of the class, and there would be ambiguity if the constructor of that class takes an argument – which constructor should be called by JVM and what parameters should be passed? We know that JVM can’t instantiate a Java class without calling a constructor method.

The below example demonstrates why the main() method is static in Java:

public class MainMethodDemo() {

    public MainMethodDemo ( int arg0) {
        //One argument constructor
    }

    public MainMethodDemo (int arg0, int arg1) {
        //Two arguments constructor
    }

    public MainMethodDemo (String arg[]) {
    }

    public void main(String…args) {
        //Non Static main method
    }
}

JDK: Java Development Kit is a software development kit to develop or run java programs(applications). JDK contains JRE along with compiler, debugger, JavaDoc and keytool.

JRE: Java Runtime Environment is used to run java applications. It contains JVM along with class libraries.

JVM: Java Virtual Machine is the engine that drives the java code by converting  java byte code into machine code.

 

JDK is for development purpose whereas JRE is for running the java programs.

JDK and JRE both contains JVM so that we can run our java program.

JVM is the heart of java programming language and provides platform independence.

There are seven qualities to be satisfied for a programming language to be pure Object Oriented:

1.Encapsulation/Data Hiding

2.Inheritance

3.Polymorphism

4.Abstraction

5.All predefined types are objects

6.All user defined types are objects

7.All operations performed on objects must be only through methods exposed at the objects.