Java Streams – Answers

Here, you can find the questions for which answers are provided here. http://talks.skilltoz.com/streams-questions/

Stream Basics

  1. Choice A is correct. The above code prints “sel els” when run. The chars() method returns an IntStream representing the characters. The distinct() method removes the duplicates, then the resulting character stream is boxed to actual characters. A peek() call at this point returns the characters ‘s’, ‘e’ and ‘l’ only. After sorting the order becomes ‘e’, ‘l’ and then ‘s’. Hence A is correct and the other choices are incorrect.
  2. C is the correct answer. Here the empty stream is created by calling Stream.empty() and passed to the printCollection() method because the integer array is null. As there is no data in the stream, there is no output.
  3. D is the correct answer. The code causes IllegalStateException to be thrown. A stream builder has a life cycle, which starts in a building phase, during which elements can be added, and then transitions to a built phase, after which elements may not be added. Here, the accept() method is also called after the build() method is invoked, this causes the exception.
  4. A will run without errors and print 123 twice because operations on streams do not change the source, hence multiple streams can be opened from the same source. B will cause IllegalStateException with the message “stream has already been operated upon or closed” because of trying to read from the same stream twice.
  5. a and b are correct choices. The anyMatch() method is used to check if any of the elements in the stream matches the predicate condition. This is applied using lambda expression in choice A and using method reference syntax in choice b. Choices c and d are incorrect because the allMatch() method returns true only if every element of the stream matches the predicate condition.
  6. D is the correct choice. When this program is executed, nothing is printed to the console. That is because intermediate operations are lazy and will only be executed when a terminal operation is present. In this case, each filter() method call is an intermediate operation, but there is no terminal operation.
  7. B is the correct answer. Calling the map method twice on the Stream myStream causes an exception with the message “The stream has already been operated upon or closed”. A stream should be operated on (invoking an intermediate or terminal stream operation) only once. 
  8. When List.of() method is called, the resultant list is immutable. Hence, UnsupportedOperationException is thrown when replaceAll() method is invoked on the list. Option D is the correct answer. Arrays.asList() method returns a fixed length list, but you can replace it’s elements. Option A, B and C are incorrect because the syntax is incorrect.

Primitive Streams

9. Choices A and C are correct because the average() method is defined only for primitive streams like IntStream. The range(1,5) method of IntStream returns the integers from 1 to 4. Choice B and D are incorrect because compile time errors result in trying to invoke average() method on non-primitive streams and lists.

Skipping and Limiting Streams

10. A is the correct answer. The filter() method results in the even numbers alone to be included in the stream which is fed to the next stage. When skip(3) is called, the first three even numbers 2,4 and 6 are skipped. The next even number 8 is then sent to the map() method which then returns the square of the number, which is 64. B is incorrect because skip(5) will cause 8 also to be skipped. C is incorrect because limit(3) would cause an output of [4 16 36] because the limit() method only restricts the count of elements and hence returns the square of the first 3 elements which are even, thus printing [4, 16, 36]. D is incorrect because limit(5) would cause an output of [4, 16, 36] because the limit() method restricts the count of elements to 5 and hence returns the square of the first 4 elements, thus printing [4, 16, 36, 64]. 

Primitive Streams

11. Choice C is correct. The boxed() method needs to be invoked at line no: 5 for the code to compile without errors. Here, we need to convert primitive values to their wrapper equivalents so to get a Stream on which the collect() method can be invoked . Choices A, B and D are incorrect because these do not convert IntSream to Stream<Integer>.

Debugging Streams using peek()

12. Choices A and B are correct. These will compile and run without errors. Choice C will cause IllegalStateException to be thrown because a Stream should be operated on (invoking an intermediate or terminal stream operation) only once. A Stream implementation may throw IllegalStateException if it detects that the Stream is being reused. Choice D will not even compile because peek() method cannot be called without any arguments.

Follow the below link to find the questions for which answers are provided here. http://talks.skilltoz.com/streams-questions/

Leave a Reply

Your email address will not be published. Required fields are marked *