Review Questions
16.1 Given the following code:
import java.util.*; public class RQ1 { public static void main(String[] args) { List<String> values = Arrays.asList("X", "XXX", "XX", "XXXX"); int value = values.stream() .mapToInt(v -> v.length()) .filter(v -> v != 4) .reduce(1, (x, y) -> x * y); System.out.println(value); } }
What is the result?
Select the one correct answer.
4
6
24
The program will throw an exception at runtime.
16.2 Which statement is true about the Stream methods?
The filter() method discards elements from the stream that match the given Predicate.
The findFirst() method always returns the first element in the stream.
The reduce() method removes elements from the stream that match the given Predicate.
The sorted() method sorts the elements in a stream according to their natural order, or according to a given Comparator.
16.3 Given the following code:
import java.util.stream.*; public class RQ3 { public static void main(String[] args) { IntStream values = IntStream.range(0, 5); // (1) INSERT CODE HERE System.out.println(sum); } }
Which of the following statements when inserted independently at (1) will result in a compile-time error?
Select the two correct answers.
int sum = values.reduce(0, (x, y) -> x + y);
int sum = values.parallel().reduce(0, (x, y) -> x + y);
int sum = values.reduce((x, y) -> x + y).orElse(0);
int sum = values.reduce(0, (x, y) -> x + y).orElse(0);
int sum = values.parallel().reduce((x, y) -> x + sum).orElse(0);
int sum = values.sum();
16.4 Given the following code:
import java.util.stream.*; public class RQ4 { public static void main(String[] args) { IntStream values = IntStream.range(0, 5); // (1) INSERT CODE HERE System.out.println(value); } }
Which of the following statements, when inserted independently at (1), will result in the value 4 being printed?
Select the two correct answers.
int value = values.reduce(0, (x, y) -> x + 1);
int value = values.reduce((x, y) -> x + 1).orElse(0);
int value = values.reduce(0, (x, y) -> y + 1);
int value = values.reduce(0, (x, y) -> y);
int value = values.reduce(1, (x, y) -> y + 1);
long value = values.count();
16.5 Given the following code:
import java.util.*; import java.util.stream.*; public class RQ5 { public static void main(String[] args) { List<String> values = List.of("AA", "BBB", "C", "DD", "EEE"); Map<Integer, List<String>> map = null; // (1) INSERT CODE HERE map.forEach((i, s) -> System.out.println(i + " " + s)); } }
Which statement when inserted independently at (1) will result in the output 1 [C]?
Select the one correct answer.
-
map = values.stream() .collect(Collectors.groupingBy(s -> s.length(), Collectors.filtering(s -> !s.contains("C"), Collectors.toList())));
-
map = values.stream() .collect(Collectors.groupingBy(s -> s.length(), Collectors.filtering(s -> s.contains("C"), Collectors.toList())));
-
map = values.stream() .filter(s -> !s.contains("C")) .collect(Collectors.groupingBy(s -> s.length(), Collectors.toList()));
-
map = values.stream() .filter(s -> s.contains("C")) .collect(Collectors.groupingBy(s -> s.length(), Collectors.toList()));
16.6 Given the following code:
import java.util.stream.*; public class RQ7 { public static void main(String[] args) { Stream<String> values = Stream.generate(() -> "A"); boolean value = values.peek(v -> System.out.print("B")) .takeWhile(v -> !v.equals("A")) .peek(v -> System.out.print("C")) .anyMatch(v -> v.equals("A")); System.out.println(value); } }
What is the result?
Select the one correct answer.
Btrue
Ctrue
BCtrue
Bfalse
Cfalse
BCfalse
16.7 Given the following code:
import java.util.stream.*; public class RQ9 { public static void main(String[] args) { IntStream.range('a', 'e') .mapToObj(i -> String.valueOf((char) i).toUpperCase()) .filter(s -> "AEIOU".contains(s)) .forEach(s -> System.out.print(s)); } }
What is the result?
Select the one correct answer.
A
AE
BCD
The program will fail to compile.
16.8 Given the following code:
import java.util.stream.*; public class RQ10 { public static void main(String[] args) { IntStream.range(0, 5) .filter(i -> i % 2 != 0) .forEach(i -> System.out.println(i)); } }
Which of the following statements will produce the same result as the program? Select the two correct answers.
-
IntStream.rangeClosed(0, 5) .filter(i -> i % 2 != 0) .forEach(i -> System.out.println(i));
-
IntStream.range(0, 10) .takeWhile(i -> i < 5) .filter(i -> i % 2 != 0) .forEach(i -> System.out.println(i));
-
IntStream.range(0, 10) .limit(5) .filter(i -> i % 2 != 0) .forEach(i -> System.out.println(i));
-
IntStream.generate(() -> {int x = 0; return x++;}) .takeWhile(i -> i < 4) .filter(i -> i % 2 != 0) .forEach(i -> System.out.println(i));
-
var x = 0; IntStream.generate(() -> return x++) .limit(5) .filter(i -> i % 2 != 0) .forEach(i -> System.out.println(i));
16.9 Given the following code:
import java.util.function.*; import java.util.stream.*; public class RQ11 { public static void main(String[] args) { Stream<String> abc = Stream.of("A", "B", "C"); Stream<String> xyz = Stream.of("X", "Y", "Z"); String value = Stream.concat(xyz, abc).reduce((a, b) -> b + a).get(); System.out.println(value); } }
What is the result?
Select the one correct answer.
ABCXYZ
XYZABC
ZYXCBA
CBAZYX
16.10 Which statement produces a different result from the other statements?
Select the one correct answer.
-
Stream.of("A", "B", "C", "D", "E") .filter(s -> s.compareTo("B") < 0) .collect(Collectors.groupingBy(s -> "AEIOU".contains(s))) .forEach((x, y) -> System.out.println(x + " " + y));
-
Stream.of("A", "B", "C", "D", "E") .filter(s -> s.compareTo("B") < 0) .collect(Collectors.partitioningBy(s -> "AEIOU".contains(s))) .forEach((x, y) -> System.out.println(x + " " + y));
-
Stream.of("A", "B", "C", "D", "E") .collect(Collectors.groupingBy(s -> "AEIOU".contains(s), Collectors.filtering(s -> s.compareTo("B") < 0, Collectors.toList()))) .forEach((x, y) -> System.out.println(x + " " + y));
-
Stream.of("A", "B", "C", "D", "E") .collect(Collectors.partitioningBy(s -> "AEIOU".contains(s), Collectors.filtering(s -> s.compareTo("B") < 0, Collectors.toList()))) .forEach((x, y) -> System.out.println(x + " " + y));
16.11 Given the following code:
import java.util.stream.*; public class RQ13 { public static void main(String[] args) { Stream<String> strings = Stream.of("i", "am", "ok").parallel(); IntStream chars = strings.flatMapToInt(line -> line.chars()).sorted(); chars.forEach(c -> System.out.print((char)c)); } }
What is the result?
Select the one correct answer.
iamok
aikmo
amiok
The result from running the program is unpredictable.
The program will throw an exception at runtime.
16.12 Which of the following statements are true about the Stream methods? Select the two correct answers.
The filter() method accepts a Function.
The peek() method accepts a Function.
The peek() method accepts a Consumer.
The forEach() method accepts a Consumer.
The map() method accepts a Predicate.
The max() method accepts a Predicate.
The findAny() method accepts a Predicate.
16.13 Which Stream methods are terminal operations? Select the two correct answers.
peek()
forEach()
map()
filter()
sorted()
min()
16.14 Which Stream methods have short-circuit execution? Select the two correct answers.
collect()
limit()
flatMap()
anyMatch()
reduce()
sum()
16.15 Given the following code:
import java.util.stream.*; public class RQ17 { public static void main(String[] args) { Stream<String> values = Stream.of("is", "this", "", null, "ok", "?"); // (1) INSERT CODE HERE System.out.println(c); } }
Which statement inserted independently at (1) produces the output 6?
Select the one correct answer.
long c = values.count();
long c = values.collect(Collectors.counting());
int c = values.mapToInt(v -> 1).reduce(0, (x, y) -> x + 1);
long c = values.collect(Collectors.reducing(0L, v -> 1L, Long::sum));
int c = values.mapToInt(v -> 1).sum();
Insert any of the above.
16.16 Which code produces identical results? Select the two correct answers.
-
Set<String> set1 = Stream.of("XX", "XXXX", "", null, "XX", "X") .filter(v -> v != null) .collect(Collectors.toSet()); set1.stream() .mapToInt(v -> v.length()) .sorted() .forEach(v -> System.out.print(v));
-
Set<Integer> set2 = Stream.of("XX", "XXXX", "", null, "XX", "X") .map(v -> (v == null) ? 0 : v.length()) .filter(v -> v != 0) .collect(Collectors.toSet()); set2.stream() .sorted() .forEach(v -> System.out.print(v));
-
List<Integer> list1 = Stream.of("XX", "XXXX", "", null, "XX", "X") .map(v -> (v == null) ? 0 : v.length()) .filter(v -> v != 0) .toList(); list1.stream() .sorted() .forEach(v -> System.out.print(v));
-
List<Integer> list2 = Stream.of("XX", "XXXX", "", null, "XX", "X") .map(v -> (v == null) ? 0 : v.length()) .filter(v -> v != 0) .distinct() .toList(); list2.stream() .sorted() .forEach(v -> System.out.print(v));