Java OCA OCP Practice Question 2981

Question

Given this code segment:

final CyclicBarrier barrier =
         new CyclicBarrier(3, () -> System.out.println("Let's play"));
             // LINE_ONE

Runnable r = () -> {                            // LINE_TWO
    System.out.println("Awaiting");
    try {//  w w  w .j a v a2 s . c om
        barrier.await();
    } catch(Exception e) { /* ignore */ }
};

Thread t1 = new Thread(r);
Thread t2 = new Thread(r);
Thread t3 = new Thread(r);

t1.start();
t2.start();
t3.start();

Choose the correct option based on this code segment.

a)   this code segment results in a compiler error in line marked with the
     comment LINE_ONE/*from   w  w  w. j a v  a  2 s  . co m*/

b)   this code segment results in a compiler error in line marked with the
     comment LINE_TWO

c)   this code prints:

     Let's play

d)   this code prints:

     Awaiting
     Awaiting
     Awaiting
     Let's play

e)   this code segment does not print anything on the console


d)

Note

there are three threads expected in the CyclicBarrier because of the value 3 given as the first argument to the CyclicBarrier constructor.

When a thread executes, it prints "Awaiting" and awaits for the other threads (if any) to join.

once all three threads join, they cross the barrier and the message "Let's play" gets printed on the console.




PreviousNext

Related