Java Interview Question and Answer.

 

In this post, we are going to discuss some important Java Interview Questions and Answers.

Difference between final, finally, finalize.?

We’re going to take an overview of final, finally and finalize Java keywords.

 

Final

Final is a modifier applicable for classes, methods, and variables. 

If a class declared as a final then we can’t extend that class. i.e we can’t create child class for that class.

If a method declared as a final then we can’t override that method in the child class.

If a variable declared as a final then it will become as constant and we can’t perform re-assignment for that variable.

 

Finally

Finally is a block always associated with try-catch to maintain cleanup code?

Let’s discuss this with an example.

try{
//risky code...
}
catch(X e){
//handling code
}
finally{
//cleanup cod
}

 

In the above example in try block, we are writing all risky code. When we think if any exception will occur in that line than we write that code in try block.

In catch block, we are writing exception handling code. And in finally block we are writing cleanup code like DataBase connection closed and any important code,

which we want to print anyhow means either exception occurs or not it will always execute.

 

Finalize

Finalize is a method (finalize()) present in object class. It is always invoked by garbage collector just before destroying an object to perform the cleanup activity.

 

Different between finally and Finalize.

Finally meant for cleanup activities related to try block. Whereas finalize() meant for cleanup activities related to the object.

 

Which class is a root java Exception hierarchy?

Throwable act as a root java for java Exception hierarchy. It has two child class Exception and Error.

 

Difference between Exception and Error

Exception

Most of the exception of the cases are caused by our program and this is recoverable.

Exceptions are recoverable. Let’s understand with an example.

try{
//Read data from a file location at techFile
}
catch(FileNotFoundException e){
//use location file and continue rest of the program normally
}

In this example, if our program requirement to read data from a remote file location at techfile. At runtime, if the tech file is not available then we will get fileNotFoundException.

If fileNotFoundException occurs then we can provide a local file and the rest of the program will be executed normally.

 

Error

Most of the time errors are not caused by our program these are due to the system resources. and it is not recoverable. Error is not recoverable. Let’s understand with an example.

For Example, if outOfMemory error occurs beings a programer we can not do anything and the program will be terminated abnormally.

outOfMemory problem will be solved by the System admin or server admin by increasing heap memory.

 

Difference between Checked Exception and Unchecked Exception

Checked Exception:

The Exception which is checked by the compiler for smooth execution of the program at runtime is called a checked exception.

Example : FileNotFoundException

In case of checked exception, the compiler will check whether we are handling exception or not, if the programmer not handling then we will get a compile-time error.

 

Unchecked Exception:

The Exception which is not checked by the compiler is called an Unchecked exception.

Example: AirthmeticException, NullPinterException

In the case of Unchecked exception, the compiler won’t check whether the programmer handling exception or not.

 

Note 1:- Whether exception is Checked or Unchecked compulsory it will occur only at runtime.

Note 2:- Runtime exception and it’s child classes error and it’s child classes are Unchecked exception and the remaining all are Checked exception.

 

Difference between Full Checked Exception and Partially Checked Exception?

Full Checked Exception:

A Checked exception is said to be fully the checked exception if and only if all it’s child classes also checked.

Example: InputOutputException, IntrupException.

 

Partially Checked Exception:

A Checked exception is said to be partially checked exception if and only if some of it’s child classes also Unchecked.

Example: Exception.

Note:- There are only Two partially checked exceptions in java.

  1. Exception
  2. Throwable

 

 

Controle Flow of try-catch block

Example:

try {
statement1;
statement2;
statement3;
}
catch{
statement4;
}
statement4;

 

Case 1: If there is no exception then it’s execution is.

statement1;
statement2;
statement3;
statement4; Normal termination.

 

Case 2: If an exception raised at statement2 and corresponding catch block match then it’s execution is.

statement1;
statement4;
statement5; Normal termination.

 

Case 3: If an exception raised at statement2 and the corresponding catch block not match then it’s execution is.

statement1; Abnormal termination.

 

Case 4: If an exception raised at statement4 or statements5.

It is always an Abnormal termination.

 

Conclusion 1: Within the try block, if anywhere exception raised, then the rest of the try block won’t be executed, even though we handle that exception. Hence the length of the try block should be as less as possible. And we have to write only risky code in the try block.

Conclusion 2: In addition to try block there may be a chance raising an exception in the catch blocks and finally bocks.

Conclusion 3: If any statement raises an exception wich, not a try block then it always Abnormal termination.

 

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *