1. What is the difference between JDK, JRE, and JVM?

JVM JVM is an acronym for Java Virtual Machine; it is an abstract machine which provides the runtime environment in which Java bytecode can be executed. It is a specification which specifies the working of Java Virtual Machine. Its implementation has been provided by Oracle and other companies. Its implementation is known as JRE. JVMs are available for many hardware and software platforms (so JVM is platform dependent). It is a runtime instance which is created when we run the Java class. There are three notions of the JVM: specification, implementation, and instance.
JRE JRE stands for Java Runtime Environment. It is the implementation of JVM. The Java Runtime Environment is a set of software tools which are used for developing Java applications. It is used to provide the runtime environment. It is the implementation of JVM. It physically exists. It contains a set of libraries + other files that JVM uses at runtime.
JDK JDK is an acronym for Java Development Kit. It is a software development environment which is used to develop Java applications and applets. It physically exists. It contains JRE + development tools. JDK is an implementation of any one of the below given Java Platforms released by Oracle Corporation

2.Can you print any statement without using main method in JAVA?

Ans: Yes , we can print any statement without using main method .We can print a statement by using static blocks but this was allowed till JDK1.7 .From JDK1.8 the code would not execute because it looks for a main method before any other thing.

3. What is JIT compiler?

Just-In-Time(JIT) compiler: It is used to improve the performance. JIT compiles parts of the bytecode that have similar functionality at the same time, and hence reduces the amount of time needed for compilation. Here the term “compiler” refers to a translator from the instruction set of a Java virtual machine (JVM) to the instruction set of a specific CPU.

4.What is the difference between String s = "Test" and String s = new String("Test")? Which is better and why?

Ans: In general, String s = "Test" is more efficient to use than String s = new

5.Nested classes can be static or non-static (also called an inner class). How do you decide which to use? Does it matter?

Ans:The key difference between is that inner classes have full access to the fields and methods of the enclosing class. This can be convenient for event handlers, but comes at a cost: every instance of an inner class retains and requires a reference to its enclosing class. With this cost in mind, there are many situations where we should prefer static nested classes. When instances of the nested class will outlive instances of the enclosing class, the nested class should be static to prevent memory leaks.

6.Is it possible to import same package or class twice? Will the JVM load the package twice at runtime?

Ans: It is possible to import the same package or class more than once. It will not have any effect on the compiler or JVM. The JVM will load the class more than once, irrespective of the number of times you import the same class.

7.How can we restrict inheritance for a class?

We can restrict inheritance for class by following steps.
By using final keyword
If we make all method final, then we cannot override that.
By using private constructors
By using Javadoc comment (//)

8.Are constructors inherited? Can a subclass call the parent's class constructor?

We cannot inherit a constructor. We create an instance of a subclass using a constructor of one of its superclass. Because override the superclass constructor is not our wish so that, we override a superclass constructor, then we destroy the encapsulation abilities of the language.

9. Do you agree with the statement “There should be only one main method in a file or a program ”.Justify your answer.

Ans: No there is no limit for the main methods. Each class can have its own main method and For every class , A .class file is generated

10.Can you override a private or static method in Java?

Ans:You cannot override a private or static method in Java, if you create a similar method with same return type and same method arguments in child class then it will hide the superclass method, this is known as method hiding. Similarly, you cannot override a private method in sub class because it's not accessible there, what you do is create another private method with the same name in the child class.

11.What is the difference between StringBuffer and StringBuilder in Java?

Ans:StringBuilder in Java was introduced in JDK 1.5 and the only difference between both of them is that StringBuffer methods e.g. length(), capacity() or append() are synchronized while corresponding methods in StringBuilder are not synchronized. Because of this fundamental difference, concatenation of String using StringBuilder is faster than StringBuffer. Actually, it's considered the bad practice to use StringBuffer anymore, because, in almost 99% scenario, you perform string concatenation on the same thread.

12.What is an alternative to achieve multiple inheritance in java?

Ans:Interfaces provide an alternative to multiple inheritance. Java programming language does not support multiple inheritance. But interfaces provide a good solution. Any class can implement a particular interface and importantly the interfaces are not a part of class hierarchy. So, the general rule is extend one but implement many. A class can extend just one class but it can implement many interfaces. So, here we have multiple types for a class. It can be of the type of its super class and all the interfaces it implements.

13.How Do You Connect to a Database in Java?

The steps to connect to a database in Java are:
• Registering the driver class
• Creating connection
• Creating statement
• Executing queries
• Closing connection

14.Is an exception occurred in one thread causes other threads to terminate.?

No, exception is thread wise. Only that thread will terminate in which exception has occurred. Other threads will continue to execute.

15.Can we access non-static members of outer class inside a static nested class?

No, we can’t access non-static members of outer class inside a static nested class. We can access only static members of outer class inside a static nested class.

16. What are Wrapper classes?

Java wrapper classes are the Object representation of eight primitive types in java. All the wrapper classes in java are immutable and final. Java 5 autoboxing and unboxing allows easy conversion between primitive types and their corresponding wrapper classes.