Java Interview Questions

What are the rules for casting primitive types ?

You can cast any non Boolean type to any other non boolean type
You cannot cast a boolean to any other type; you cannot cast any other type to a boolean

What are the rules for object reference assignment and method call conversion?

An interface type can only be converted to an interface type or to object. If the new type is an interface, it must be a superinterface of the old

type.  A class type can be converted to a class type or to an interface type. If converting to a class type the new type should be superclass of the old type. If

converting to an interface type new type the old class must implement the interface
An array maybe converted to class object, to the interface cloneable, or to an array. Only an array of object references types may be converted to

an array, and the old element type must be convertible to the new element

When do we say an exception is handled ?

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

When do we say an exception is not handled ?

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

READ  Super classes and subclasses - Java Tutorial

In what sequence does the finally block gets executed ?

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

What can prevent the execution of the code in finally block ?

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

What does throws statement declaration in a method indicate ?

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

What are checked exception ?

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

What are runtime exceptions ?

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

What is difference between Exception and errors ?

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

How will you handle the checked exceptions ?

You can provide a try/catch block to handle it.
Make sure method declaration includes a throws clause that informs the calling method an exception might be thrown from this particular

READ  Parallel Operations in Java 8


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

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

Yes it is perfectly legal