Java OCA OCP Practice Question 2942

Question

Which of the following are possible results of running the following application? (Choose all that apply.)

import java.util.concurrent.locks.*; 
import java.util.stream.IntStream; 
? 
public class Main { 
   public static void main(String[] args) { 
      Lock lock = new ReentrantLock(); 
      IntStream.iterate(1, i -> 1).limit(10).parallel().forEach(x -> { // w1 
         lock.tryLock(); // w2 
         System.out.println("Got Lock!"); 
         lock.unlock(); // w3 
      }); /*from w  w  w  . j  av a2  s . c o m*/
      System.out.print("Finished"); 
   } 
} 
  • A. It prints Got Lock!at least one time.
  • B. It prints Got Lock! ten times.
  • C. It prints Finished.
  • D. It hangs indefinitely at runtime.
  • E. The code will not compile because of line w1.
  • F. The code will not compile because of line w2.
  • G. The code will not compile because of line w3.
  • H. It throws an exception at runtime.


A,B,C,H.

Note

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

The tryLock() method attempts to obtain a lock but returns immediately with a value of false if the lock cannot be acquired.

The application does not check whether or not the lock was actually acquired; therefore the call to unlock() on w3 would produce an IllegalMonitorStateException at runtime if a thread that did not get a lock attempted to release it, and H is correct.

The answer A is also correct, because the first thread to run will obtain the lock without issue.

This behavior is indeterminate, though.

If the threads are processed in order, with all of the threads able to obtain the lock, then the code will complete without throwing an exception, making B and C also correct.

Finally, D is incorrect since tryLock() returns immediately if it cannot obtain the lock.

Note that this code does not use a try/finally block to ensure that the unlock() method is called whenever a lock is obtained.

Although using try/finally is strongly recommended, it is not required for the code to compile and run.




PreviousNext

Related