Java OCA OCP Practice Question 2848

Question

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

import java.util.concurrent.*; 
public class Main extends RecursiveAction { 
   private int start; 
   private int end; 
   public Main(int start, int end) { 
      this.start = start; 
      this.end = end; 
   } /*from   w ww  . j a va 2 s  .c  o m*/
   protected void compute() { 
      if (start<0) return; 
     else { 
        int middle = start + ((end-start) / 2); 
        invokeAll(new Main(start, middle),   
           new Main(middle, end)); // m1 
     } 
  } 
  public static void main(String[] args) { 
     ForkJoinTask<?> task = new Main(0, 4); // m2 
     ForkJoinPool pool = new ForkJoinPool(); 
     Object result = pool.invoke(task); // m3 
  } 
} 
  • A. It compiles and runs without issue.
  • B. The code will not compile because of m1.
  • C. The code will not compile because of m2.
  • D. The code will not compile because of m3.
  • E. It compiles but throws an exception at runtime.
  • F. It compiles but hangs at runtime.


E.

Note

The program compiles without issue, so B, C, and D are incorrect.

Lines m2 and m3 throw a compiler warning about generics but still compile.

Notice that RecursiveAction, unlike RecursiveTask, does not return a value.

However, since we used a generic ForkJoinTask reference, the code still compiles.

The issue here is that the base condition is not reached since the numbers start/end are consistently positive.

This causes an infinite loop, although since memory is finite, Java detects this and throws a StackOverflowError, so E is correct.

In practice, this could also generate a locking exception before the StackOverflowError when the program runs out of memory, but in either circumstance, the program will exit.




PreviousNext

Related