Java OCA OCP Practice Question 3059

Question

Choose the correct option based on this code segment:

Stream<Integer> ints = Stream.of(1, 2, 3, 4);
boolean result = ints.parallel().map(Function.identity()).isParallel();
System.out.println(result);
  • a) this code segment results in compiler error(s)
  • b) this code segment throws InvalidParallelizationException for the call parallel()
  • c) this code segment prints: false
  • d) this code segment prints: true


d)

Note

the stream pointed by ints is a sequential stream because sequential is the default execution mode.

the call to parallel() method changes the execution mode to parallel stream.

the isParallel() method returns true because the current execution mode of the stream is parallel.

option a) this code compiles without errors.

the call to map(Function.identity()) is acceptable because the argument Function.identity() just returns the same stream element it is passed with.

option b) It is possible to change the execution mode of a stream after it is created, and it does not result in throwing any exceptions.

option c) the isParallel() method returns the current execution mode and not the execution mode when the stream was created.

So the isParallel() method returns true in this code (and not false as given in this option).




PreviousNext

Related