Java Thread Interview Questions and Answers

    6 Votes

19) What is meant by Deadlock ?

Deadlock arises when two threads get the same set of locks. But the order of locks is different. As a result of this, the first thread will be waiting for the second thread to release the lock, meanwhile second thread is waiting for the first thread to release the lock. As a result of this, both threads will in a suspended state. Synchronization should be used to avoid Deadlock.

20) What is race condition in Java ?

A race condition is a situation, when two threads access the same location in memory at the same time, and at least one of the accesses is a write.

21) Can you explain about Static method Synchronization and Instance method synchronization?

Locks applied on the static method and non static methods are different. Locks are applied on the class in case of static methods. In case of non-static methods, locks are applied on the instance of the class. For better understanding, look at the example below

Static method Synchronization :

static synchronized void mymethod()

equivalent to

static void mymethod()  

Instance method synchronization :

synchronized void mymethod()
equivalent to

void mymethod()

22) Differentiate Exception and error in Java ?

Derived from java.lang.Exception Derived from java.lang.Error
Indicates conditions that an application might catch. Indicates serious problems that an application should not catch
Ex: FileNotFoundException. Ex: OutOfMemoryError

23) Checked vs Unchecked Exceptions in Java ?

Checked ExceptionsUnchecked Exceptions
Checked at compile time. Checked at runtime
Method must either handle the exception or it must specify the exception using throws keyword. Error and RuntimeException classes are unchecked exceptions.
Expected to recover from an exception. It cannot do anything to recover from the exception.
Ex: FileNotFoundException. Ex: ArithmeticException

24) Can you explain difference between Throws and Throw ?

Used to declare an exception. Used to throw an exception explicitly
Throw is followed by an instance variable Throws is followed by exception class name
Throw is used inside method body to invoke an exception. Throws clause is used in method declaration
Cannot throw more than one exception Can declare multiple exceptions.
Ex: throw new Exception("Something went wrong!"); Ex : public void mathFunction() throws IOException,ArithmeticException

25) Can you explain about finally keyword in Java ?

  • Finally is used for exception handling along with try and catch.
  • The code in finally block will execute even if an exception occurs.
  • A finally block appears at the end of try or try-catch block.

26) Is it possible to have try block without finally and catch block ?

No. This will result in a compilation error. A try block must be followed by a catch or a finally block. We can avoid catch or the finally block, but not both.

27) Can you explain about situation where getting a compilation error exception already caught ?

When we specify multiple catch statements, make sure that the most specific one is put on top,followed by more generic ones at the end. If you put the more generic ones at the beginning, an exception will be caught multiple times and hence getting a compilation error "exception already caught".

Page 3 of 4

Popular Videos


How to improve your Interview, Salary Negotiation, Communication & Presentation Skills.

Got a tip or Question?
Let us know

Related Articles

Java OOPS Interview Questions and Answers
Java Interview Questions
Top Advanced Java Interview Questions and Answers
Java Collections Interview Questions and Answers