Chapter 10 Streams Flashcards
Review Questions
1. What could be the output of the following?
var stream = Stream.iterate("", (s) -> s + "1"); System.out.println(stream.limit(2).map(x -> x + "2"));
A. 12112
B. 212
C. 212112
D. java.util.stream.ReferencePipeline$3@4517d9a3
E. The code does not compile.
F. An exception is thrown.
G. The code hangs.
2. What could be the output of the following?
Predicate<String> predicate = s -> s.startsWith("g"); var stream1 = Stream.generate(() -> "growl!"); var stream2 = Stream.generate(() -> "growl!"); var b1 = stream1.anyMatch(predicate); var b2 = stream2.allMatch(predicate); System.out.println(b1 + " " + b2);
A. true false
B. true true
C. java.util.stream.ReferencePipeline$3@4517d9a3
D. The code does not compile.
E. An exception is thrown.
F. The code hangs.
3.What could be the output of the following?
~~~
Predicate<String> predicate = s -> s.length()> 3;
var stream = Stream.iterate("-",
s -> ! s.isEmpty(), (s) -> s + s);
var b1 = stream.noneMatch(predicate);
var b2 = stream.anyMatch(predicate);
System.out.println(b1 + " " + b2);
~~~</String>
A. false false
B. false true
C. java.util.stream.ReferencePipeline$3@4517d9a3
D. The code does not compile.
E. An exception is thrown.
F. The code hangs.
5.Which of the following sets result to 8.0? (Choose all that apply.)
A.
~~~
double result = LongStream.of(6L, 8L, 10L)
.mapToInt(x -> (int) x)
.collect(Collectors.groupingBy(x -> x))
.keySet()
.stream()
.collect(Collectors.averagingInt(x -> x));
~~~
B.
~~~
double result = LongStream.of(6L, 8L, 10L)
.mapToInt(x -> x)
.boxed()
.collect(Collectors.groupingBy(x -> x))
.keySet()
.stream()
.collect(Collectors.averagingInt(x -> x));
~~~
C.
~~~
double result = LongStream.of(6L, 8L, 10L)
.mapToInt(x -> (int) x)
.boxed()
.collect(Collectors.groupingBy(x -> x))
.keySet()
.stream()
.collect(Collectors.averagingInt(x -> x));
~~~
D.
~~~
double result = LongStream.of(6L, 8L, 10L)
.mapToInt(x -> (int) x)
.collect(Collectors.groupingBy(x -> x, Collectors.toSet()))
.keySet()
.stream()
.collect(Collectors.averagingInt(x -> x));
~~~
E.
~~~
double result = LongStream.of(6L, 8L, 10L)
.mapToInt(x -> x)
.boxed()
.collect(Collectors.groupingBy(x -> x, Collectors.toSet()))
.keySet()
.stream()
.collect(Collectors.averagingInt(x -> x));
~~~
F.
~~~
double result = LongStream.of(6L, 8L, 10L)
.mapToInt(x -> (int) x)
.boxed()
.collect(Collectors.groupingBy(x -> x, Collectors.toSet()))
.keySet()
.stream()
.collect(Collectors.averagingInt(x -> x));
~~~
6.Which of the following can fill in the blank so that the code prints out false? (Choose all that apply.)
~~~
var s = Stream.generate(() -> “meow”);
var match = s.______(String::isEmpty);
System.out.println(match);
~~~
A. allMatch
B. anyMatch
C. findAny
D. findFirst
E. noneMatch
F. None of the above
4.Which are true statements about terminal operations in a stream that runs successfully? (Choose all that apply.)
A. At most one terminal operation can exist in a stream pipeline.
B. Terminal operations are a required part of the stream pipeline in order to get a result.
C. Terminal operations have Stream as the return type.
D. The peek() method is an example of a terminal operation.
E. The referenced Stream may be used after calling a terminal operation.
7.We have a method that returns a sorted list without changing the original. Which of the following can replace the method implementation to do the same with streams?
~~~
private static List<String> sort(List<String> list) {
var copy = new ArrayList<String>(list);
Collections.sort(copy, (a, b) -> b.compareTo(a));
return copy;
}
~~~</String></String></String>
A.
~~~
return list.stream()
.compare((a, b) -> b.compareTo(a))
.collect(Collectors.toList());
~~~
B.
~~~
return list.stream()
.compare((a, b) -> b.compareTo(a))
.sort();
~~~
C.
~~~
return list.stream()
.compareTo((a, b) -> b.compareTo(a))
.collect(Collectors.toList());
~~~
D.
~~~
return list.stream()
.compareTo((a, b) -> b.compareTo(a))
.sort();
~~~
E.
~~~
return list.stream()
.sorted((a, b) -> b.compareTo(a))
.collect();
~~~
F.
~~~
return list.stream()
.sorted((a, b) -> b.compareTo(a))
.collect(Collectors.toList());
~~~
8.Which of the following are true given this declaration? (Choose all that apply.)var is = IntStream.empty();
A. is.average() returns the type int.
B. is.average() returns the type OptionalInt.
C. is.findAny() returns the type int.
D. is.findAny() returns the type OptionalInt.
E. is.sum() returns the type int.
F. is.sum() returns the type OptionalInt.
9.Which of the following can we add after line 6 for the code to run without error and not produce any output? (Choose all that apply.)
~~~
4: var stream = LongStream.of(1, 2, 3);
5: var opt = stream.map(n -> n * 10)
6: .filter(n -> n < 5).findFirst();
~~~
A.
~~~
if (opt.isPresent())
System.out.println(opt.get());
~~~
B.
~~~
if (opt.isPresent())
System.out.println(opt.getAsLong());
~~~
C.
~~~
opt.ifPresent(System.out.println);
~~~
D.
~~~
opt.ifPresent(System.out::println);
~~~
E. None of these; the code does not compile.
F. None of these; line 6 throws an exception at runtime.
10.Given the four statements (L, M, N, O), select and order the ones that would complete the expression and cause the code to output 10 lines. (Choose all that apply.)
~~~
Stream.generate(() -> “1”)
L: .filter(x -> x.length()> 1)
M: .forEach(System.out::println)
N: .limit(10)
O: .peek(System.out::println)
;
~~~
A. L, N
B. L, N, O
C. L, N, M
D. L, N, M, O
E. L, O, M
F. N, M
G. N, O
12.Which is true of the following code?
~~~
Set<String> birds = Set.of("oriole", "flamingo");
Stream.concat(birds.stream(), birds.stream(), birds.stream())
.sorted() // line X
.distinct()
.findAny()
.ifPresent(System.out::println);
~~~</String>
A. It is guaranteed to print flamingo as is and when line X is removed.
B. It is guaranteed to print oriole as is and when line X is removed.
C. It is guaranteed to print flamingo as is, but not when line X is removed.
D. It is guaranteed to print oriole as is, but not when line X is removed.
E. The output may vary as is.
F. The code does not compile.
G. It throws an exception because the same list is used as the source for multiple streams.
11.What changes need to be made together for this code to print the string 12345? (Choose all that apply.)
~~~
Stream.iterate(1, x -> x++)
.limit(5).map(x -> x)
.collect(Collectors.joining());
~~~
A. Change Collectors.joining() to
Collectors.joining(“,”).
B. Change map(x -> x) to map(x -> “” + x).
C. Change x -> x++ to x -> ++x.
D. Add .forEach(System.out::print) after the call to
collect().
E. Wrap the entire line in a System.out.print statement.
F. None of the above. The code already prints 12345.
13.Which of the following is true?
~~~
List<Integer> x1 = List.of(1, 2, 3);
List<Integer> x2 = List.of(4, 5, 6);
List<Integer> x3 = List.of();
Stream.of(x1, x2, x3).map(x -> x + 1)
.flatMap(x -> x.stream())
.forEach(System.out::print);
~~~</Integer></Integer></Integer>
A. The code compiles and prints 123456.
B. The code compiles and prints 234567.
C. The code compiles but does not print anything.
D. The code compiles but prints stream references.
E. The code runs infinitely.
F. The code does not compile.
G. The code throws an exception.
14.Which of the following are true? (Choose all that apply.)
~~~
4: Stream<Integer> s = Stream.of(1);
5: IntStream is = s.boxed();
6: DoubleStream ds = s.mapToDouble(x -> x);
7: Stream<Integer> s2 = ds.mapToInt(x -> x);
8: s2.forEach(System.out::print);
~~~</Integer></Integer>
A. Line 4 causes a compiler error.
B. Line 5 causes a compiler error.
C. Line 6 causes a compiler error.
D. Line 7 causes a compiler error.
E. Line 8 causes a compiler error.
F. The code compiles but throws an exception at runtime.
G. The code compiles and prints 1.
15.Given the generic type String, the partitioningBy() collector creates a Map<Boolean, List<String>>
when passed to collect() by default. When a downstream collector is passed to partitioningBy(), which return types can be created? (Choose all that apply.)
A. Map<boolean, List<String>>
B. Map<Boolean, List<String>>
C. Map<Boolean, Map<String>>
D. Map<Boolean, Set<String>>
E. Map<Long, TreeSet<String>>
F. None of the above