Java OCA OCP Practice Question 1965

Question

What is the output of the following application?.

1:  package mypkg; 
2:  import java.util.concurrent.*; 
3:  public class Main { 
4:     public void m() { 
5:        ExecutorService service = Executors.newCachedThreadPool(); 
6:        Future bosses = service.submit(() -> System.out.print("")); 
7:        service.shutdown(); //  w ww  . ja  v a2  s  . c  om
8:        System.out.print(bosses.get()); 
9:     } 
10:    public static void main(String[] memo) { 
11:       new Main().m(); 
12:    } 
13: } 
  • A. null
  • B. The code does not compile.
  • C. Line 7 throws an exception at runtime.
  • D. Line 8 throws an exception at runtime.


B.

Note

The class does not compile because the Future.get() on line 8 throws a checked InterruptedException and ExecutionException, neither of which is handled nor declared by the m() method.

If the m() and accompanying main() methods were both updated to declare these exceptions, then the application would print null at runtime, and Option A would be the correct answer.

Future can be used with Runnable lambda expressions that do not have a return value but that the return value is always null when completed.




PreviousNext

Related