Exception and Thread Java Interview Questions
Categories: Education
Q.1. What is the difference between Error and Exception?
Ans. An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors you cannot repair them at runtime. Though error can be caught in the catch block but the execution of application will come to a halt and is not recoverable.
While exceptions are conditions that occur because of bad input or human error etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving the user feedback for entering proper values etc.
Q.2. How can you handle Java exceptions?
Ans. There are five keywords used to handle exceptions in Java:
(i) try
(ii) catch
(iii) finally
(iv) throw
(v) throws
Q.3. What are the differences between Checked Exception and Unchecked Exception?
Ans.
Checked Exception
(i) The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions.
(ii) Checked exceptions are checked at compile-time.
(iii) Example: IOException, SQLException etc.
Unchecked Exception
(i) The classes that extend RuntimeException are known as unchecked exceptions.
(ii) Unchecked exceptions are not checked at compile-time.
(iii) Example: ArithmeticException, NullPointerException etc.
Q.4. What are the different ways of thread usage?
Ans. There are two ways to create a thread:
(i) Extending Thread class
This creates a thread by creating an instance of a new class that extends the Thread class. The extending class must override the run() function, which is the thread’s entry point.
(ii) Implementing Runnable interface
This is the easiest way to create a thread, by creating a class that implements the runnable interface. After implementing the runnable interface, the class must implement the public void run() method ()
- The run() method creates a parallel thread in your programme. When run() returns, the thread will come to an end.
- The run() method creates a parallel thread in your programme. When run() returns, the thread will come to an end.
- Within the run() method, you must specify the thread’s code.
- Like any other method, the run() method can call other methods, use other classes, and define variables.
Q.5. Java works as “pass by value” or “pass by reference” phenomenon?
Ans. Java is always pass-by-value. This means that it creates a copy of the contents of the parameter in memory. In Java, object variables always refer to the memory heap’s real object.
Q.6. Will the finally block get executed when the return statement is written at the end of try block and catch block as shown below?
Ans. The finally block always gets executed even hen the return statement is written at the end of the try block and the catch block. It always executes , whether there is an exception or not. There are only a few situations in which the finally block does not execute, such as VM crash, power failure, software crash, etc. If you don’t want to execute the finally block, you need to call the System.exit() method explicitly in the finally block.
Q.7. How does an exception propagate in the code?
Ans. If an exception is not caught, it is thrown from the top of the stack and falls down the call stack to the previous procedure. If the exception isn’t caught there, it falls back to the previous function, and so on, until it’s caught or the call stack reaches the bottom. The term for this is Exception propagation.
Q.8. Can you explain the Java thread lifecycle?
Ans. The java thread lifecycle has the following states-
New-:
When a thread is created, and before the program starts the thread, it is in the new state. It is also referred to as a born thread.
Runnable:
When a thread is started, it is in the Runnable state. In this state, the thread is executing its task.
Waiting:
Sometimes, a thread goes to the waiting state, where it remains idle because another thread is executing. When the other thread has finished, the waiting thread again comes into the running state.
Timed Waiting:
In timed waiting, the thread goes to waiting state. But, it remains in waiting state for only a specified interval of time after which it starts executing.It remains waiting either till the time interval ends or till the other thread has finished.
Terminated:
A thread is said to be in this state once it terminates. It may be because the thread has completed its task or due to any other reason.
Q.9. What is exception hierarchy in java?
Ans. The hierarchy is as follows:
Throwable is a parent class of all Exception classes. There are two types of Exceptions: Checked exceptions and UncheckedExceptions or RunTimeExceptions. Both type of exceptions extends Exception class whereas errors are further classified into Virtual Machine error and Assertion error.
Q.10. How to create a custom Exception?
Ans. To create you own exception extend the Exception class or any of its subclasses.
class New1Exception extends Exception { } // this will create Checked Exception
class NewException extends IOException { } // this will create Checked exception
class NewException extends NullPonterExcpetion { } // this will create UnChecked exception