Java OCA OCP Practice Question 2960

Question

What is the result of the following program? (Choose all that apply.)

import java.util.concurrent.*; 
import java.util.concurrent.locks.*; 
? 
public class Main { 
   public static void goSwimming(Lock lock) { 
      try { //  w w  w .  j a v  a  2  s .co m
         lock.lock(); // y1 
         if(lock.tryLock()) { // y2 
           System.out.println("Got Lock"); 
         } 
      } finally { 
         lock.unlock(); 
      } 
   } 
   public static void main(String[] args) { 
      Lock lock = new ReentrantLock(); 
      ExecutorService service = null; 
      try { 
         service = Executors.newFixedThreadPool(2); 
         for(int i=0; i<2; i++) 
            service.submit(() -> goSwimming(lock)); 
      } finally { 
         if (service != null) service.shutdown(); 
      } 
      System.out.print("Tasks Complete"); 
   } 
} 
A.   It prints Got Lockat least one time.
B.   It prints Got Lock exactly twice.
C.   It prints Tasks Complete.
D.   It hangs indefinitely at runtime.
E.   The code will not compile because of line y1.
F.   The code will not compile because of line y2.
G.   It throws an exception at runtime.


A,C,D.

Note

The application compiles without issue, so E and F are incorrect.

The most important thing to notice is that the goSwimming() method performs two lock requests, via lock() and tryLock(), but it has only one call to unlock().

Recall that for Reentrant locks, a thread must call unlock() the same number of times it locks the object, or else the lock will not be released.

Therefore, only one thread is able to acquire the lock and print Got Lock For these reasons, A is correct and B is incorrect.

C is also correct, since the lock requests are performed on separate threads from the main execution thread.

Since the lock is never released by the first thread, the second thread will hang indefinitely, making D a correct answer.

Finally, H is incorrect, because this code does not throw any exceptions at runtime.




PreviousNext

Related