Java OCA OCP Practice Question 2280

Question

Which statements about the following application are true?

package mypkg; /*from   w w w .j a va  2s.c o  m*/
import java.util.concurrent.*; 
public class Plan { 
   private ExecutorService service = Executors.newCachedThreadPool(); 
   public void planEvents() { 
      service.scheduleWithFixedDelay( 
            () -> System.out.print("A"), 
            1, TimeUnit.HOURS); 
      service.scheduleAtFixedRate( 
            () -> System.out.print("B"), 
            1, 1000, TimeUnit.SECONDS); 
      service.execute(() -> System.out.print("C")); 
   } 
} 
  • I. The scheduleWithFixedDelay() method call compiles.
  • II. The scheduleAtFixedRate() method call compiles.
  • III. The execute() method call compiles.
  • A. I only
  • B. II only
  • C. III only
  • D. I and II
  • E. I, II, and III
  • F. None of the above


C.

Note

Both schedule method calls do not compile because these methods are only available in the ScheduledExecutorService interface, not the ExecutorService interface.

Even if the correct reference type for service was used, along with a compatible Executors factory method, the scheduleWithFixedDelay() call would still not compile because it only contains a single numeric value.

This method requires two long values, one for the initial delay and one for the period.

The execute() method call compiles without issue because this method is available in the ExecutorService interface.

For these reasons, only the third statement is true, making Option C the correct answer.




PreviousNext

Related