Java 8+ Features
Difficulty: hard
Overview
Lambda expressions: concise anonymous function syntax (params) -> body. Stream API: lazy, pipeline-based operations — filter(), map(), flatMap(), reduce(), collect(). Intermediate ops (filter, map) are lazy; terminal ops (collect, forEach, reduce) trigger evaluation. Optional<T> wraps nullable values to avoid NullPointerException. Method references: ClassName::method. Functional interfaces in java.util.function: Predicate<T>, Function<T,R>, Consumer<T>, Supplier<T>. Collectors.groupingBy() returns Map<K, List<V>>.
Practice Linked Questions
Q1. A lambda expression in Java is best described as:
Select one answer before revealing.
Q2. What does Stream.filter() do in Java?
Select one answer before revealing.
Q3. What is the difference between map() and flatMap() in Java Streams?
Select one answer before revealing.
Q4. The Optional<T> class in Java (introduced in Java 8) is used to:
Select one answer before revealing.
Q5. Which of the following are terminal operations on a Java Stream? (More than one answer may be correct)
Select one answer before revealing.
Q6. Which is the correct syntax for a method reference in Java?
Select one answer before revealing.
Q7. Collectors.groupingBy() in Java Streams returns:
Select one answer before revealing.
Q8. Which of the following are functional interfaces in the java.util.function package? (More than one answer may be correct)
Select one answer before revealing.
Q9. The reduce() terminal operation in Java Streams:
Select one answer before revealing.
Q10. What is the output of the following code? List<Integer> nums = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5)); nums.sort((a, b) -> b - a); System.out.println(nums);
Select one answer before revealing.
Q11. What is the output of the following code? List<String> words = Arrays.asList("apple", "banana", "cherry", "date"); List<String> result = words.stream() .filter(w -> w.length() > 4) .map(String::toUpperCase) .collect(Collectors.toList()); System.out.println(result);
Select one answer before revealing.
Q12. What is the output of the following code? Optional<String> opt = Optional.ofNullable(null); String result = opt.orElse("default"); System.out.println(result); System.out.println(opt.isPresent());
Select one answer before revealing.
Q13. What is the output of the following code? Runnable r = () -> System.out.println("Running!"); r.run();
Select one answer before revealing.
Q14. What is the output of the following code? int result = IntStream.rangeClosed(1, 5) .reduce(0, Integer::sum); System.out.println(result);
Select one answer before revealing.
Q15. Which of the following Stream operations are intermediate (lazy) operations? (More than one answer may be correct) A: stream.filter(x -> x > 0) B: stream.count() C: stream.map(x -> x * 2) D: stream.collect(Collectors.toList())
Select one answer before revealing.
Q16. What is the output of the following code? List<String> list = Arrays.asList("Java", null, "Python"); long count = list.stream() .filter(Objects::nonNull) .map(String::length) .count(); System.out.println(count);
Select one answer before revealing.
Q17. What is the output of the following code? List<String> words = Arrays.asList("ant", "bear", "cat", "ape", "bat"); Map<Character, List<String>> grouped = words.stream() .collect(Collectors.groupingBy(w -> w.charAt(0))); System.out.println(grouped.get('a'));
Select one answer before revealing.
Q18. What is the output of the following code? List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); names.stream() .map(String::length) .forEach(System.out::println);
Select one answer before revealing.
Q19. What is the output of the following code, and does it compile? int base = 10; Function<Integer, Integer> adder = x -> x + base; System.out.println(adder.apply(5));
Select one answer before revealing.
Q20. What is the output of the following code? List<String> words = Arrays.asList("Hello", "World", "Java"); String result = words.stream() .collect(Collectors.joining(", ", "[", "]")); System.out.println(result);
Select one answer before revealing.
Q21. What is the output of the following code? List<Integer> list = Arrays.asList(5, 3, 8, 1, 9, 2); Optional<Integer> max = list.stream().max(Integer::compareTo); System.out.println(max.get());
Select one answer before revealing.