Java OCA OCP Practice Question 1689

Question

How many of the following can fill in the blank to have the code print 44?

Stream<String> stream = Stream.of("base", "ball");
stream.___(s -> s.length()).forEach(System.out::print);
I.  map
II.  mapToInt
III.  mapToObject
  • A. None
  • B. One
  • C. Two
  • D. Three


C.

Note

The map() method can fill in the blank.

The lambda converts a String to an int and Java uses autoboxing to turn that into an Integer.

The mapToInt() method can also fill in the blank and Java doesn't even need to autobox.

There isn't a mapToObject() in the stream API.

Note there is a similarly named mapToObj() method on IntStream.

Since both map() and mapToInt() work here, Option C is correct.




PreviousNext

Related