Saturday 28 April 2012

JAVA interview questions 24

Q231.Is it possible to take try, catch inside catch block? 
Ans. Yes, It is possible to take try, catch inside catch block. 

Q232. Is it possible to take try without catch? 
Ans. Yes, it is possible to take try without catch but compulsory finally block should be available. 

Q233. What is the purpose of finally block? 
Ans. The main purpose of finally block is, to maintain the cleanup code. This block will execute always. 

Q234. Is finally block will be execute always? 
Ans. Yes finally block will be executed always irrespective of whether exception raised or not raised whether exceptions are handled or not handle. There is one situation where the finally block won’t be executed if the JVM is going to be shutdown. 

Q235. In which situation finally block will not executed? 
Ans. There is one situation where the finally block won’t be executed if we are using 
system.exit(0) explicitly then JVM itself will be shutdown and there is no chance of executing finally block. 

Q236. If return statement present inside try is finally block will be executed? 
Ans. Yes, if return statement present inside try, then also finally block will be executed. finally block will dominate return statement also. 

Q237. What is the difference between final, finally and finalize()? 
Ans. final:- final is a modifier applicable for variables, methods and classes. final variable 
means constant and reassignment is not possible. final method means implementation is final in the child classes we can’t override. final class means it won’t participate in inheritance and child class creation is not possible. 
finally:- It is a block associated with try catch to maintain cleanup code. Finally block will be executed always irrespective of whether exception is raised or not raised or whether the exception is handle or not handle. 
finalize():- It is a method, Garbage collector always calls this method just before destroying any object to perform cleanup activities. 

Q238. Is it possible to write any statement between try-catch and finally? 
Ans. No, it is not possible to write any statement between try catch and finally. If we will try to write any statement between them then we will get compile time error. 

Q239. Is it possible to take two finally blocks for the same try? 
Ans. No, it is not possible to take two finally blocks for the same try. If we try to take then we will get compile time error. 

Q240. Is syntax try-finally-catch is valid ? 
Ans. No, this syntax is not valid. It should be like try-catch-finally then only code will compile.

No comments:

Post a Comment