Java Interview Questions and Answers for 3 Years Experience - 2

Question: 6

What is the difference between exception and error?

The exception class defines mild error conditions that your program encounters.

Ex: Arithmetic Exception, FileNot Found exception.

Exceptions can occur when try to open the file, which does not exist the network connection is disrupted.

Operands being manipulated are out of prescribed ranges.

The class file you are interested in loading is missing the error class defines serious error conditions that you should not attempt to recover from.

In most cases it is advisable to let the program terminate when such an error is encountered.

Question: 7

What are the different kinds of exceptions?

There are two types of exceptions.

Checked exception

Unchecked exception

Checked exception is cached at the compile time while unchecked exception is checked at run time.

Checked Exception:

Environmental error that cannot necessarily be detected by testing; e.g. disk full, broken socket, database unavailable etc.

Unchecked Exception:

Virtual machine error: class not found, out of memory, no such method, illegal access to private field, etc.

Runtime Exception:

Programming errors that should be detected in testing: index out of bounds, null pointer, illegal argument etc.

Question: 8

What is the difference between final, finally and finalize()?

final:

It is a modifier which can be applied to a class, method or variable.

It is not possible to inherit the final class, override the final method and change the final variable.

finally:

It is an exception handling code section.

It gets executed whether an exception is raised or not by the try block code segment.

finalize()

It is a method of Object class.

It is executed by the JVM just before garbage collecting object to give a final chance for resource releasing activity.

Out of the three, only finally is related to java exception handling.

Question: 9

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.

Errors are generally irrecoverable conditions.

Question: 10

Is it necessary that each try block must be followed by a catch block?

It is not necessary that each try block must be followed by a catch block.

It should be followed by either a catch block OR a finally block.

And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

Related Questions