Java OCA OCP Practice Question 2272

Question

What is the output of executing the following code snippet?

30: ExecutorService e = Executors.newSingleThreadExecutor(); 
31: Runnable r1 = () -> Stream.of(1,2,3).parallel(); 
32: Callable r2 = () -> Stream.of(4,5,6).parallel(); 
33: /*from  w w  w. j a  v  a  2  s.c  om*/
34: Future<Stream> f1 = e.submit(r1); 
35: Future<Stream> f2 = e.submit(r2); 
36: 
37: Stream<Integer> s = Stream.of(f1.get(),f2.get()) 
38:       .flatMap(p -> p) 
39:       .parallelStream(); 
40: 
41: ConcurrentMap<Boolean,List<Integer>> r = 
42:       s.collect(Collectors.groupingByConcurrent(i -> i%2==0)); 
43: System.out.println(r.get(false).size()+" "+r.get(true).size()); 
  • A. 3 3
  • B. 2 4
  • C. The code does not compile due to one error.
  • D. The code does not compile due to two errors.
  • E. The code does not compile due to three errors.
  • F. The code compiles but a NullPointerException is thrown at runtime.


D.

Note

Line 34 does not compile because of an assignment and value mismatch.

The r1 variable is a Runnable expression.

While there is an ExecutorService.submit() that takes a Runnable expression, it returns Future<?> since the return type is void.

This type is incompatible with the Future<Stream> assignment without an explicit cast, leading to a compiler error.

Next, line 39 does not compile.

The parallelStream() method is found in the Collection interface, not the Stream interface.

Due to these two compilation errors, Option D is the correct answer.




PreviousNext

Related