Java Deadlocks and Synchronization

Here are 10 essential multiple-choice questions on Java Deadlocks and Synchronization, covering key concepts.

Last Updated :
Discuss
Comments

Question 1

What is the key difference between the volatile keyword and the synchronized keyword in Java?

  • volatile can be used for blocking threads, while synchronized cannot

  • volatile ensures visibility of variables across threads, while synchronized ensures mutual exclusion

  • volatile guarantees atomicity, while synchronized does not

  • volatile prevents deadlocks, while synchronized does not

Question 2

Which of the following is the most common cause of a deadlock in multithreading?

  • Two threads accessing the same variable

  • Threads requesting resources in a different order

  • A single thread holding multiple locks

  • Not using volatile correctly

Question 3

Which of the following statements is true about the Atomic class in Java?

  • Atomic is used to guarantee mutual exclusion for methods

  • Atomic ensures atomic operations without using synchronization

  • Atomic uses volatile for thread visibility

  • Atomic prevents race conditions by blocking threads

Question 4

What will happen if two threads try to acquire two locks in opposite order?

Java
class A {
    synchronized void method1(B b) {
        b.last();
    }
    synchronized void last() {}
}

class B {
    synchronized void method1(A a) {
        a.last();
    }
    synchronized void last() {}
}
  • One thread will succeed

  • The program will throw an exception

  • Deadlock will occur

  • Both threads will run concurrently

Question 5

What is the primary role of the synchronized keyword in Java?

  • To guarantee that the method is executed by a single thread


  • To pause threads for a set period

  • To ensure that multiple threads are executed simultaneously

  • To prevent thread starvation

Question 6

Which of the following statements about deadlocks is false?

  • Deadlock occurs when two threads hold locks and wait for each other

  • Deadlock can be avoided by acquiring locks in the same order

  • Deadlock always leads to an exception

  • Deadlock can be prevented by using tryLock() in ReentrantLock

Question 7

How can deadlock be avoided in a multithreading environment?

  • By synchronizing all methods

  • By using volatile for all variables

  • By acquiring locks in a consistent order

  • By never using locks

Question 8

What is the purpose of the ReentrantLock in Java?

  • It provides an atomic way of accessing a shared resource

  • It supports interruptible lock acquisition

  • It uses volatile variables to prevent race conditions

  • It synchronizes method calls automatically

Question 9

In the following code, which concept is demonstrated in the context of synchronization?

Java
class Geeks {
    private int counter = 0;
    public synchronized void increment() {
        counter++;
    }
    public synchronized int getCounter() {
        return counter;
    }
}


  • Use of Atomic class


  • Local framework synchronization

  • Synchronization on a method level

  • Usage of volatile keyword

Question 10

What would happen in the following situation involving ReentrantLock?

Java
Lock lock = new ReentrantLock();
lock.lock();
try {
    // critical section
    lock.lock(); // Reentrant lock acquired again
} finally {
    lock.unlock();
    lock.unlock(); // Release both locks
}
  • Throws an exception

  • Successfully acquires and releases the lock

  • Causes a deadlock

  • Deadlock prevention fails

There are 10 questions to complete.

Take a part in the ongoing discussion