Java OCA OCP Practice Question 1999

Question

Given the following code snippet,

what statement about the values printed on lines p1 and p2 is correct?.

List<Integer> db = Collections.synchronizedList(new ArrayList<>()); 
IntStream.iterate(1, i -> i+1).limit(5) 
   .parallel() 
   .map(i -> {db.add(i); return i;}) 
   .forEachOrdered(System.out::print);  // p1 

db.forEach(System.out::print);  // p2 
  • A. They are always the same.
  • B. They are sometimes the same.
  • C. They are never the same.
  • D. The code will produce a ConcurrentModificationException at runtime.


B.

Note

First off, the class uses a synchronized list, which is thread-safe and allows modification from multiple threads, making Option D incorrect.

The process generates a list of numbers from 1 to 5 and sends them into a parallel stream where the map() is applied, possibly out of order.

This results in elements being written to db in a random order.

The stream then applies the forEachOrdered() method to its elements, which will force the parallel stream into a single-threaded state.

At runtime, line p1 will print the results in order every time as 12345.

On the other hand, since the elements were added to db in a random order, the output of line p2 is random and cannot be predicted ahead of time.

Since the results may sometimes be the same, Option B is the correct answer.

Part of the reason that the results are indeterminate is that the question uses a stateful lambda expression, which based on your studies should be avoided in practice!.




PreviousNext

Related