PASS - TAKER : Anonymos

Java SE 8 OCP - Streams

Correct : 13

76% Complete (success)

76 %

False : 4

23% Complete (success)

23 %



Anonymos 2019-07-05T00:04

Correct

1. Given the code fragment: {code} Which code fragment inserted at line // L1 will compile correctly and sum the numbers in the array nums and store the value in the variable sum?

We provide an identity for reduce() so we get back an int, rather than an optional. A is incorrect because reduce() needs an identity or it creates an Optional. C and D are incorrect syntax for reduce(), and both result in compile errors.

Correct

2. Given the following code fragment: {code} and a file, "names.txt," with one name per line, which of the following code fragments inserted at line // L1 will produce a sorted List of names in the variable names?

We call sorted() to sort the stream and collect() to collect the results in a List. A, B, and C are incorrect. For A, toList() is a method of Collectors, not Stream. For B, comparing() is a method of Comparator, not Stream [you pass a Comparator to sorted()]. For C, sorted() is an intermediate stream operation that should be called before the collect() reduction, a terminal operation.

Correct

3. What are the correct types for variables s1 and s2 in the code fragment below?

The ints in the call to Stream.of() get autoboxed to Integers. (If we’d used IntStream.of() instead, then we could use IntStream as the type for s1.) Given that s1 is a Stream<Integer>, when we call mapToInt() on elements of the stream (which maps objects to ints), we are converting the Integer bjects to ints, so the type of s2 must be IntStream.

Correct

4. Given the code fragments: {code} Which code fragment could you use for CODE in the call to map() in the following code fragment: System.out.println("Duck names: "); ducks.stream().map(CODE).forEach(d -> System.out.print(d + " ")); System.out.println(); to correctly print the string (choose all that apply): Duck names: Jerry George Kramer Elaine Huey Louie Dewey

A is a method reference shorthand for C. B and D are incorrect. B has incorrect syntax for a lambda expression and method reference. D is not a lambda expression and should be.

Correct

5. Given the Duck class and ducks List: {code} What output does the following code fragment produce? ducks.stream() .filter(d -> d.getColor.().equals("mottled")) .map(d -> d.getName()) .forEach(d -> System.out.print(d + " "));

Filter all ducks who have "mottled" color and map ducks to their name. B, C, and D are incorrect. B shows all the ducks instead of just "mottled" ducks. D shows the result if you print the full Duck [via toString()] but you are mapping a duck to its name before printing.

Correct

6. Given the Duck class and ducks List: {code} Which code fragment would you use to compute the average age of the ducks as a whole number?

We stream the ducks and map each duck to its age (an int) and then reduce to the average of the ages. Note that we are using mapToInt() to map a duck to its age. average() returns an optional. A, C, D, and E are incorrect. A, C, and D don’t compile. average() returns an OptionalDouble. In D, we have an additional problem with map() because the lambda maps a duck to its age (a primitive), but map() is used to map an object to an object.

Correct

7. Given the Duck class and ducks List: {code} Which code fragment would you use to count how many mottled ducks there are?

We stream the ducks and filter for ducks whose color is "mottled." We call count() to reduce to the number of mottled ducks. B, C, and D are incorrect. B doesn’t compile [count() returns a long]. In C, d in filter is a Duck, not a String, so it will not equal "mottled" and the count will be 0. D doesn’t compile because of invalid syntax for the expected Predicate in filter()

Not Correct

8. Given the Duck class and ducks List: {code} And the following code fragment: ducks.stream() .collect(Collectors.groupingBy(d -> d.getColor())) .forEach((c, dl) -> { System.out.print("Ducks who are " + c + ": "); dl.forEach(d -> System.out.print(d.getName() + " ")); System.out.println(); }); What is the result?

We stream the ducks and group them by their color, creating a Map. We then use forEach() to take each map entry (ducks by their color) and display the Map key—the color—and the Map value, which is a list of Ducks. We further iterate through each list of ducks to display just the names. A, B, and C are incorrect. A and B are incorrect based on the above information. C is incorrect because it shows the Duck name printed as a List rather than as Strings and shows the word "color" rather than the color value of each duck (the Map key for the Map produced by Collectors.groupingBy()).

Not Correct

9. Given the Duck class and ducks List: {code} and the following code fragment: TYPE duckMap = ducks.stream() .collect(Collectors.groupingBy(d -> d.getColor(), Collectors.toList())); What is the correct type for duckMap in TYPE?

Collectors.groupingBy() creates a Map with color Strings as keys and Lists of Ducks as values.

Correct

10. Given the code fragment: {code} What is the correct TYPE for minInteger?

min() is a reduction method that produces an optional type. We start with a Stream<Integer>; mapToInt() creates an IntStream, so min() produces an OptionalInt.

Correct

11. Given the following code: {code} and the following code fragment: Comparator<Temperature> tCompare = ((t1, t2) -> t1.getTemp().compareTo(t2.getTemp())); TYPE max = julyAvgs.stream().max(tCompare); What is the correct TYPE for max?

max() takes a Comparator, which takes two Temperature objects and compares them using the temp field, producing the maximum Temperature object in the List.

Not Correct

12. Given the Temperature class and julyAvgs List: {code} and the following code fragment: TYPE maxT = julyAvgs.stream().mapToDouble(t -> t.getTemp()).max(); What is the correct TYPE for maxT?

We map each Temperature object to its (unboxed) double temp and take the max() of the stream of doubles, producing an OptionalDouble.

Correct

13. Given the Temperature class and julyAvgs List: {code} Will the minimum temperature, min, be the same as coolerSpot?

The minimum is 80.5 in Reno, Nevada. findAny() finds any Temperature from the filtered stream of Temperatures < 100; for a nonparallel stream, it’s usually the first one, 85.3 in Salt Lake City, but there is no guarantee that findAny() will return the first Temperature in the filtered stream.

Correct

14. Given the Temperature class and julyAvgs List: {code} and the following code fragment: Comparator<Temperature> tCompare = ((t1, t2) -> t1.getTemp().compareTo(t2.getTemp())); List<Temperature> sorted Temps = julyAvgs.stream() // L1 sortedTemps.forEach(System.out::println); Which fragment(s), inserted independently at // L1, cause the code to print the Temperatures from high temp to low temp?

We stream the Temperatures using the tCompare Comparator, which sorts the Temperatures from low to high based on the temp value, so we then reverse that ordering with reversed(). Then we collect the results in a List. A, B, and C are incorrect. A has two problems: Temperature isn’t Comparable, and reversed() is a method on Comparators, so this code will not compile. B and C work but print the temperatures from low to high instead of high to low.

Correct

15. Given the Temperature class and julyAvgs List: {code} and the following code fragment: julyAvgs.stream() .mapToLong(t -> Math.round(t.getTemp())) .sorted() .distinct() .forEach(t -> System.out.println(t + " ")); What does this code display?

We stream the Temperatures and map each temperature to a long value by rounding the Temperature’s temp value. We then remove any duplicates by calling distinct() (eliminating the repeated 81 value) and then print them out. A, B, and D are incorrect. For A, notice in the code fragment that we are using Math.round() to cut off the decimal points (so we process longs, not doubles). We are using distinct() to eliminate duplicates, so we shouldn’t see 81 twice as we do in B. D is sorted in the wrong order; natural order is ascending for long values.

Correct

16. Given the Temperature class and julyAvgs List: {code} What does the following code fragment display? Map<Boolean, List<String>> temp100 = julyAvgs.stream().collect( Collectors.partioningBy(t -> t.getTemp() >= 100.0, Collectors.mapping(t -> t.getLocation()), Collectors.toList()))); System.out.println(temp100);

We are creating a partition, so we will get (and display) a Map with two keys, true and false, partitioned by whether the temp value of a Temperature is >= 100.0. Each Temperature is mapped (downstream) to its location and the locations for each partition are collected into a List. The locations are Strings, so we see a List of String locations for each partition.

Not Correct

17. Given the Temperature class and julyAvgs List: {code} Which of the following fragments will print Temperature objects sorted by location?

We sort the Temperature objects by location and print. sorted() takes a Comparator. A, B, and D are incorrect. A and D display only locations, not the whole Temperature object. B produces a compile error because sorted() requires a Comparator, and we’ve provided a Function.