Java Interview Questions
1 .
What is the purpose of default constructor?
Default constructor provides the default values to the object like 0, null etc. depending on the type.
2 .
Can constructor perform other tasks instead of initialization?
Yes, like object creation, starting a thread, calling method etc. You can perform any operation in the constructor as you perform in the method.
3 .
In how many ways can you create an object in java?
There are 4 ways of creating objects in java.

1.Using new operator :

Employee obj=new Employee();

Here we are creating Employee class object ‘obj’ using new operator.

2.Using factory methods :

NumberFormat obj=NumberFormat.getNumberInstance();

Here, we are creating NumberFormat class object using factory method getNumberInstance().

3. Using newInstance() method. Here, we should follow two steps, as:

a.First, store the class name ‘Employee’ as a String into a object. For this purpose, factory method forName() of the class ‘Class’ will be useful.

Class c=Class.forName(“Employee”);

b.Next, create another object to the class whose name is in the object c. For this purpose, we need newInstance() method of the class ‘Class’, as:

Employee obj=(Employee)c.newInstance();

4.By cloning a already available object, we can create another object. Creating exact copy of a existing object is called ‘cloning’.

Employee obj1=new Employee();
Employee obj2=(Employee)obj1.clone();
4 .
Why instance variables are not available to static methods?
After executing static methods, Jvm creates the objects. So the instance variables are not available to static methods.
5 .
Is it possible to compile and run a java program without writing main( ) method?
Yes, it is possible by using a static block in the java program.
6 .
What is the difference between primitive data types and advanced data types?
Primitive data types represent single value. Advanced data types represent a group of values. Also, methods are not available to handle the primitive data types. In case of advanced data types, methods are available to perform various operations.
7 .
Can you declare a class as abstract and final also?
No. abstract class needs sub classes. final keyword represents sub classes which cannot be created. So, both are quite contradictory and cannot be used for same class.
8 .
Can you implement one interface from another?
No, we cannot. Implementing an interface means writing body for the methods. This cannot be done again in an interface, since none of the methods of the interface can have body.
9 .
What is the difference between an exception and an error?
An exception is an error which can be handled. It means when an exception happens, the programmer can do something to avoid any harm. But an error is an error which cannot be handled, it happens and the programmer cannot do anything. 
10 .
What are checked exceptions?
The exceptions detected by java compiler at compilation time are called checked exceptions.

Example : IOException, ClassNotFoundException.

Exception Hierarchy :
All exception classes are subtypes of the java.lang.Exception class. The exception class is a subclass of the Throwable class. Other than the exception class there is another subclass called Error which is derived from the Throwable class. Exception Hierarchy is clearly shown in the figure below.