Wednesday, February 4, 2009

Exceptions interview questions

<-Pre HomeNext->
Best Exceptions interview questions.

What is exception.
An exception is an event that occurs during the execution of a program at run time.An Exception can be caught and recovered: ex.-ArrayIndexOutOfBoundsException means you tried to access a position of an Array that does not exist. Other frequently occure exception during programing are ClassCastException, NullPointerException, IllegalStateException, ArithmeticException etc.

Exceptions are those which can be handled at the run time. where as errors cannot be handled.Exceptions can be handled by using try - catch.

What is the Advantage of Exception.
Exceptions provide the means to separate the details of what to do when something out of the ordinary happens from the main logic of a program.

What classes of exceptions may be caught by a catch clause?
A catch clause can catch any exception that may be assigned to the Throwable type. This includes the Error and Exception types.

What is error.
Errors are irrecoverable exceptions.
Usually a program terminates when an error is encountered.
ex.- Error such as OutOfMemory Error.

What is the difference between checked exception and unchecked exception.
Checked exceptions are types of exceptions, which are beyond the control of programs.say I/O exception, RemoteException.This could happen, when the resource you were looking was not found(e.g file missing or remote server is down).
Unchecked exceptions are Runtime excpetions, which happens due to programmatic errors,wrong data etc.,

checked Exceptions must be dealt with in either a try/catch block or by
declaring a "throws" in a method.
Unchecked exceptions normally are Runtime exceptions like NullPointerException or ClassCastException.

^Back to top


What is the difference between throw and throws keywords? *IMP*
The throw keyword denotes a statement that causes an exception to be initiated. It takes the Exception object to be thrown as an argument. The exception will be caught by an enclosing try-catch block.
The throws keyword is a modifier of a method that denotes that an exception may be thrown by the method. An exception can be rethrown.

What are the frequent Exception's encountered because of improper coding?
java.lang:
ArithmeticException, ClassCastException, IllegalAccessException, IndexOutOfBoundsException, NullPointerException, IllegalStateException.

java.util:
NoSuchElementException

java.io:
FileNotFoundException, IOException, StreamCorruptedException, InvalidClassException,
InvalidObjectException

java.net:
SocketException, SocketTimeoutException, BindException

java.rmi:
ConnectException, NoSuchObjectException, UnexpectedException

java.text:
ParseException

What is the base class for Error and Exception?
Throwable
Does the code in finally block get executed if there is an exception and a return statement in a catch block? *IMP*

The finally clause is used to provide the capability to execute code no matter whether or not an exception is thrown or caught. If an exception occurs and there is a return statement in catch block, the finally block is still executed. The finally block will not be executed when the System.exit(0) statement is executed earlier or on system shut down earlier or the memory is used up earlier before the thread goes to finally block.

^Back to top

Does the order of placing catch statements matter in the catch block?
Yes. The FileNoFoundException is inherited from the IOException. So FileNoFoundException is caught before IOException. Exception’s subclasses have to be caught first before the General Exception




Pre->object-serialization

Next->diff. JAVA & C++

*****Please feel free to give feedback at shrawan.iitg@gmail.com and comments below *****

No comments:

Post a Comment