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 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 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.

  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 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 bocks.

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

 

Read more topics related to java

 

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

 

 

15 Replies to “Java Interview Question and Answer.”

  1. I needed to draft you one little bit of observation so as to say thank you once again for those incredible opinions you’ve documented here. It is quite pretty generous of people like you to deliver freely exactly what a number of people could possibly have made available as an e book to end up making some cash for themselves, most importantly given that you could possibly have done it if you ever decided. These inspiring ideas also served like the fantastic way to realize that some people have the identical keenness similar to my personal own to realize significantly more when it comes to this matter. I believe there are several more pleasant sessions up front for people who scan your site.

  2. I needed to post you that little bit of observation to say thanks once again for your personal marvelous principles you’ve featured in this article. It is quite seriously generous of people like you to provide unreservedly what exactly numerous people would have sold for an electronic book to help with making some profit on their own, principally since you might well have tried it in the event you wanted. Those things likewise worked to become a great way to recognize that someone else have the same passion the same as my very own to see very much more with regard to this problem. I know there are some more pleasurable situations up front for those who start reading your website.

  3. My wife and i have been really more than happy when Louis managed to finish off his research using the precious recommendations he made through your web site. It’s not at all simplistic just to always be freely giving facts a number of people could have been making money from. And now we acknowledge we’ve got the blog owner to appreciate because of that. All of the explanations you’ve made, the easy blog menu, the friendships you aid to create – it’s all amazing, and it’s aiding our son and our family believe that that article is amusing, and that’s especially essential. Many thanks for all!

  4. I precisely had to say thanks all over again. I’m not certain the things I could possibly have created in the absence of the actual hints discussed by you concerning such concern. It actually was a real depressing condition for me personally, but considering a new professional fashion you handled it forced me to cry for delight. I’m grateful for the assistance and as well , have high hopes you comprehend what a powerful job you’re carrying out educating men and women by way of your blog. I am certain you’ve never come across all of us.

  5. I enjoy you because of all of your effort on this blog. Ellie enjoys doing internet research and it’s easy to see why. All of us hear all about the compelling manner you create functional ideas by means of the blog and as well cause contribution from other ones about this article and my child is being taught a great deal. Enjoy the remaining portion of the new year. You’re the one carrying out a fantastic job.

  6. I’m commenting to let you be aware of of the awesome discovery my child developed browsing the blog. She came to find a good number of details, including how it is like to have a great coaching character to get a number of people clearly completely grasp a number of advanced issues. You actually did more than her desires. I appreciate you for churning out such beneficial, dependable, edifying and cool guidance on that topic to Emily.

  7. My wife and i were very satisfied when Peter managed to finish up his survey while using the precious recommendations he received when using the weblog. It is now and again perplexing to simply possibly be offering tips which usually some people might have been trying to sell. We see we now have you to thank for this. These illustrations you made, the straightforward website navigation, the relationships your site make it possible to promote – it is mostly terrific, and it is making our son in addition to us recognize that this theme is satisfying, which is certainly unbelievably serious. Thanks for the whole thing!

  8. I precisely needed to appreciate you once again. I am not sure the things that I might have handled in the absence of the entire points provided by you on such a problem. It has been a very frightful dilemma in my circumstances, however , noticing the skilled mode you processed that made me to jump over fulfillment. I am happier for this help and in addition hope that you know what a great job you are always getting into instructing others through the use of your web blog. I know that you have never encountered all of us.

  9. I simply wished to thank you very much yet again. I do not know what I would have used without those tips and hints shown by you relating to this theme. This has been the traumatic setting in my view, nevertheless viewing your well-written technique you treated the issue took me to weep with joy. Now i am thankful for your work as well as wish you recognize what an amazing job your are undertaking training the others with the aid of your website. Probably you haven’t met any of us.

  10. I happen to be writing to make you understand what a wonderful experience our princess went through visiting yuor web blog. She realized too many things, with the inclusion of what it is like to possess an incredible teaching style to make the rest without problems understand various very confusing topics. You undoubtedly surpassed visitors’ expected results. Thanks for supplying those warm and helpful, trustworthy, revealing not to mention unique tips on your topic to Tanya.

  11. I simply needed to thank you so much once again. I am not sure the things that I would’ve made to happen without the type of smart ideas revealed by you over such a question. It seemed to be a very frustrating issue in my opinion, however , observing the very specialised strategy you resolved that made me to leap over gladness. I am just happy for this information and then pray you realize what a great job you happen to be providing teaching people thru your web page. More than likely you have never got to know any of us.

  12. I am just writing to make you understand what a perfect experience my wife’s girl obtained using your webblog. She noticed so many pieces, which included what it is like to possess an excellent helping spirit to let others with no trouble learn about specific specialized things. You truly surpassed readers’ expected results. Thank you for providing these priceless, trusted, informative as well as fun tips about this topic to Lizeth.

Leave a Reply

Your email address will not be published.