Home Tourism Travel Hotel Restaurants Food Software Education OTHERS Multiple Choice Question (MCQ) Login

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

Top articles
Four Strategies to End Up a Transformative Educator Published at:- How could the internet provide education? Published at:- What are the advantages and disadvantages of outcome-based education? Published at:- What is the difference between training, instructing, and indoctrination? Published at:- What should be taken care of during cold days? Published at:- Which is more important: money or education? Published at:- Learning Mathematics Online Beneficial For Kids Published at:- The Importance of Emotional Intelligence in Education Published at:- The Pros and Cons of Pursuing an MBA through Distance Education Published at:- 10 Reasons Why Every Classroom Needs a Smart Board Published at:- The Benefits of Smart Boards for Student Engagement and Learning Published at:- The Role of Technology in Modern Education Published at:- How to Conduct a Successful IT Hardware Inventory Audit Published at:- Youtube Video Download Online Published at:- Best Australian Travel Books Published at:- Best Australian Travel Guides Published at:- 10 Ways Physical Education Can Improve Your Health Published at:- Exploring the Benefits of Online School Education Published at:- Exploring the Opportunities at Indira Gandhi National Open University Published at:- When Was Indira Gandhi National Open University Established Published at:- Latest Developments in Education Policy Published at:- Strategies for Developing a Successful Education Policy Published at:- When the Babri Masjid was destroyed who was the prime minister? Published at:- Top Accredited Life Coach Certification Programs Published at:- Unveiling Apple's Latest Innovation: The AI Chip Published at:- Are AI Jokes the Future of Stand-Up Comedy? Published at:- Exception and Thread Java Interview Questions Published at:- NEET (Biodiversity MCQs) Published at:- NEET (Zoology MCQs) Published at:- Exploring Career Paths for Chartered Accountants in 2024 Published at:- Business, Economy & Banking (MCQs) Published at:-

Exception and Thread Java Interview Questions