Home > Articles

This chapter is from the book

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.

  1. 4

  2. 6

  3. 24

  4. The program will throw an exception at runtime.

16.2 Which statement is true about the Stream methods?

  1. The filter() method discards elements from the stream that match the given Predicate.

  2. The findFirst() method always returns the first element in the stream.

  3. The reduce() method removes elements from the stream that match the given Predicate.

  4. 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.

  1. int sum = values.reduce(0, (x, y) -> x + y);

  2. int sum = values.parallel().reduce(0, (x, y) -> x + y);

  3. int sum = values.reduce((x, y) -> x + y).orElse(0);

  4. int sum = values.reduce(0, (x, y) -> x + y).orElse(0);

  5. int sum = values.parallel().reduce((x, y) -> x + sum).orElse(0);

  6. 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.

  1. int value = values.reduce(0, (x, y) -> x + 1);

  2. int value = values.reduce((x, y) -> x + 1).orElse(0);

  3. int value = values.reduce(0, (x, y) -> y + 1);

  4. int value = values.reduce(0, (x, y) -> y);

  5. int value = values.reduce(1, (x, y) -> y + 1);

  6. 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.

  1. map = values.stream()
                .collect(Collectors.groupingBy(s -> s.length(),
                             Collectors.filtering(s -> !s.contains("C"),
                                 Collectors.toList())));
  2. map = values.stream()
                .collect(Collectors.groupingBy(s -> s.length(),
                             Collectors.filtering(s -> s.contains("C"),
                                 Collectors.toList())));
  3. map = values.stream()
                .filter(s -> !s.contains("C"))
                .collect(Collectors.groupingBy(s -> s.length(),
                             Collectors.toList()));
  4. 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.

  1. Btrue

  2. Ctrue

  3. BCtrue

  4. Bfalse

  5. Cfalse

  6. 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.

  1. A

  2. AE

  3. BCD

  4. 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.

  1. IntStream.rangeClosed(0, 5)
             .filter(i -> i % 2 != 0)
             .forEach(i -> System.out.println(i));
  2. IntStream.range(0, 10)
             .takeWhile(i -> i < 5)
             .filter(i -> i % 2 != 0)
             .forEach(i -> System.out.println(i));
  3. IntStream.range(0, 10)
             .limit(5)
             .filter(i -> i % 2 != 0)
             .forEach(i -> System.out.println(i));
  4. IntStream.generate(() -> {int x = 0; return x++;})
             .takeWhile(i -> i < 4)
             .filter(i -> i % 2 != 0)
             .forEach(i -> System.out.println(i));
  5. 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.

  1. ABCXYZ

  2. XYZABC

  3. ZYXCBA

  4. CBAZYX

16.10 Which statement produces a different result from the other statements?

Select the one correct answer.

  1. 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));
  2. 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));
  3. 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));
  4. 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.

  1. iamok

  2. aikmo

  3. amiok

  4. The result from running the program is unpredictable.

  5. 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.

  1. The filter() method accepts a Function.

  2. The peek() method accepts a Function.

  3. The peek() method accepts a Consumer.

  4. The forEach() method accepts a Consumer.

  5. The map() method accepts a Predicate.

  6. The max() method accepts a Predicate.

  7. The findAny() method accepts a Predicate.

16.13 Which Stream methods are terminal operations? Select the two correct answers.

  1. peek()

  2. forEach()

  3. map()

  4. filter()

  5. sorted()

  6. min()

16.14 Which Stream methods have short-circuit execution? Select the two correct answers.

  1. collect()

  2. limit()

  3. flatMap()

  4. anyMatch()

  5. reduce()

  6. 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.

  1. long c = values.count();

  2. long c = values.collect(Collectors.counting());

  3. int c = values.mapToInt(v -> 1).reduce(0, (x, y) -> x + 1);

  4. long c = values.collect(Collectors.reducing(0L, v -> 1L, Long::sum));

  5. int c = values.mapToInt(v -> 1).sum();

  6. Insert any of the above.

16.16 Which code produces identical results? Select the two correct answers.

  1. 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));
  2. 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));
  3. 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));
  4. 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));

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.