Java Deadlocks and Synchronization

Last Updated :
Discuss
Comments

Question 1

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

  • A

    volatile can be used for blocking threads, while synchronized cannot

  • B

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

  • C

    volatile guarantees atomicity, while synchronized does not

  • D

    volatile prevents deadlocks, while synchronized does not

Question 2

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

  • A

    To guarantee that the method is executed by a single thread


  • B

    To pause threads for a set period

  • C

    To ensure that multiple threads are executed simultaneously

  • D

    To prevent thread starvation

Question 3

What is the purpose of the ReentrantLock in Java?

  • A

    It provides an atomic way of accessing a shared resource

  • B

    It supports interruptible lock acquisition

  • C

    It uses volatile variables to prevent race conditions

  • D

    It synchronizes method calls automatically

Question 4

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;
    }
}


  • A

    Use of Atomic class


  • B

    Local framework synchronization

  • C

    Synchronization on a method level

  • D

    Usage of volatile keyword

Question 5

Which of the following describes a deadlock situation in Java multithreading?

  • A

    When threads are not given enough CPU time

  • B

    When two threads are blocked forever, each waiting for the other

  • C

    When one thread keeps waiting for user input

  • D

    When threads stop responding to external signals

Question 6

What is a potential consequence of a deadlock in Java?

  • A

    Threads wait indefinitely and do not proceed with execution

  • B

    Threads run faster than usual

  • C

    Threads can execute without interruption

  • D

    Threads are killed automatically

Question 7

Which of the following can prevent a deadlock situation in multithreaded programming?

  • A

    Allowing only one thread to execute at a time

  • B

    Using lock ordering to acquire resources

  • C

    Synchronizing all threads in the program

  • D

    None of the above

Question 8

Which of the following is an example of a situation that can cause a deadlock in Java?



  • A

    Thread 1 holds Lock A and is waiting for Lock B, while Thread 2 holds Lock B and is waiting for Lock A

  • B

    Two threads are accessing the same resource at different times

  • C

    One thread has completed execution, and the other is waiting for its result

  • D

    Threads are executed in a random order

There are 8 questions to complete.

Take a part in the ongoing discussion