Home Interviewquestions Corejava Core Java Interview Question, Interview Question
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Core Java Interview Question, Interview Question

Advertisement
This page discusses - Core Java Interview Question, Interview Question

Core Java Interview Question Page 29

     

Flow Control and exception

Question: What is the difference between while and do while loop

Answer: Do while loop walways executes the body of the loop at least once, since the test is performed at the end of the body

Question: When do you use continue and when do you use break statements

Answer: When continue statement is applied it prematurely completes the iteration of a loop.
When break statement is applied it causes the entire loop to be abandoned.

Question: What is the base class from which all exceptions are subclasses

Answer: All exceptions are subclasses of a class called java.lang.Throwable

Question: How do you intercept and thereby control exceptions

Answer: We can do this by using try/catch/finally blocks
You place the normal processing code in try block
You put the code to deal with exceptions that might arise in try block in catch block
Code that must be executed no matter what happens must be place in finally block

Question: When do we say an exception is handled

Answer: When an exception is thrown in a try block and is caught by a matching catch block, the exception is considered to have been handled

Question: When do we say an exception is not handled

Answer: There is no catch block that names either the class of exception that has been thrown or a class of exception that is a parent class of the one that has been thrown, then the exception is considered to be unhandled, in such condition the execution leaves the method directly as if no try has been executed

Question: In what sequence does the finally block gets executed

Answer: If you put finally after a try block without a matching catch block then it will be executed after the try block
If it is placed after the catch block and there is no exception then also it will be executed after the try block
If there is an exception and it is handled by the catch block then it will be executed after the catch block

Question: What can prevent the execution of the code in finally block

Answer: 

  • The death of thread
  • Use of system.exit()
  • Turning off the power to CPU
  • An exception arising in the finally block itself

What are the rules for catching multiple exceptions
A more specific catch block must precede a more general one in the source, else it gives compilation error
Only one catch block, that is first applicable one, will be executed

Question: What does throws statement declaration in a method indicate

Answer: This indicates that the method throws some exception and the caller method should take care of handling it

Question: What are checked exception

Answer: Checked exceptions are exceptions that arise in a correct program, typically due to user mistakes like entering wrong data or I/O problems

Question: What are runtime exceptions

Answer: Runtime exceptions are due to programming bugs like out of bond arrays or null pointer exceptions.

Question: What is difference between Exception and errors

Answer: Errors are usually compile time and exceptions can be runtime or checked

Question: How will you handle the checked exceptions

Answer: You can provide a try/catch block to handle it. OR Make sure method declaration includes a throws clause that informs the calling method an exception might be thrown from this particular method. When you extend a class and override a method, can this new method throw exceptions other than those that were declared by the original method. No it cannot throw, except for the subclasses of those exceptions.

Question: Is it legal for the extending class which overrides a method which throws an exception, not o throw in the overridden class

Answer: Yes it is perfectly legal

Question: Explain the user defined Exceptions?

Answer: User defined Exceptions are the separate Exception classes defined by the user for specific purposed. An user defined can created by simply sub-classing it to the Exception class. This allows custom exceptions to be generated (using throw) and caught in the same way as normal exceptions.

Example:
 
class myCustomException extends Exception {
// The class simply has to exist to be an exception
}

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Ask Questions?    Discuss: Core Java Interview Question, Interview Question  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
sonali sinha
September 3, 2011
introduction to core java

intrview question
DMCA.com