In this article, we are going to discuss some important 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 a 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 a try block, we are writing all risky code. When we think if any exception will occur in that line then we write that code in try block.
In the catch block, we are writing exception handling code. And in the 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 the 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 tech file. 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 out of memory error occurs beings a programmer we can not do anything and the program will be terminated abnormally.
out of memory 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 exceptions, the compiler will check whether we are handling exceptions 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 an Unchecked exception, the compiler won’t check whether the programmer handling the 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 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 its child classes also checked.
Example: InputOutputException, IntrupException.
Partially Checked Exception:
A Checked exception is said to be a 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.
- Exception
- Throwable
Control Flow of try-catch block
Example:
try { statement1; statement2; statement3; } catch{ statement2; } statement4; statement5;
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 the corresponding catch block match then its execution is.
statement1;
statement4;
statement5; Normal termination.
Case 3: If an exception raised at statement2 and the corresponding catch block not match then its 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 little 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 of raising an exception in the catch blocks and finally blocks.
Conclusion 3: If any statement raises an exception which, not a try block then it always Abnormal termination.
Read more topics related to java
- Multithreading in java
- Inter thread communication in java
- synchronization in java
- Exception in java
- Inner class in java
- Different between String and String Buffer
- Top 5 string programming interview questions in java
- String in java
- Access modifiers in java
- Object-Oriented Programming (OOPS) Concept in java
Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.