Java OCA OCP Practice Question 2592

Question

Consider the following program:

import java.util.concurrent.locks.*;

public class Main {
        public static void main(String []args) {
                Lock lock = new ReentrantLock();
                try {
                        System.out.print("Lock 1 ");
                        lock.lock();//from   w  ww.  j a  v  a2  s.  c  om
                        System.out.print("Critical section 1 ");
                        System.out.print("Lock 2 ");
                        lock.lock();    // LOCK_2
                        System.out.print("Critical section 2 ");
                } finally {
                        lock.unlock();
                        System.out.print("Unlock 2 ");
                        lock.unlock();                 // UNLOCK_1
                        System.out.print("Unlock 1 ");
                }
        }
}

Which one of the following options is correct?

  • a) This program will throw an IllegalMonitorStateException in the line marked with comment LOCK_2.
  • b) This program will throw an IllegalMonitorStateException in the line marked with comment UNLOCK_1.
  • c) This program will throw an UnsupportedOperationException in the line marked with comment UNLOCK_1.
  • d) This program prints the following: Lock 1 Critical section 1 Lock 2 Critical section 2 Unlock 2 Unlock 1.


d)

Note

In a re-entrant lock, you can acquire the same lock again.

However, you need to release that lock the same number of times.




PreviousNext

Related