QuizzesJava Streams API
intermediate
java

Java Streams API

Master Java 8 Streams: pipeline operations, intermediate vs terminal operations, collectors, parallel streams, and common patterns.

25 minutes 20 questions Pass at 70%
Time Limit
25 minutes - timer starts when you begin
Change Answers Freely
Revisit and change any answer before submitting
Full Review After Submit
Correct answers and explanations shown for every question
Auto-submit on timeout
Quiz submits automatically when time runs out

Questions in this quiz

20 questions on Java Streams API. Work through them here, then start the quiz to answer them and see your score.

  1. 1.What is the Java Streams API?

    • AA network streaming framework
    • BA functional pipeline for processing sequences of elements with operations like filter, map, and collect
    • CA replacement for collections that stores data differently
    • DA library for reading and writing files
  2. 2.What is the difference between intermediate and terminal operations?

    • AIntermediate operations run immediately; terminal operations are lazy
    • BThere is no difference — all stream operations execute immediately
    • CIntermediate operations are lazy and return a Stream; terminal operations trigger execution and return a result
    • DTerminal operations can be chained; intermediate operations cannot
  3. 3.What does this code print?

    import java.util.*;
    import java.util.stream.*;
    public class Main {
        public static void main(String[] args) {
            List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
            long count = nums.stream()
                .filter(n -> n % 2 == 0)
                .count();
            System.out.println(count);
        }
    }
    • A5
    • B10
    • C4
    • D6
  4. 4.What does the map() operation do?

    • ATransforms each element to a new value using a function
    • BFilters elements matching a condition
    • CGroups elements into a Map
    • DSorts elements in natural order
  5. 5.What does this code print?

    import java.util.*;
    import java.util.stream.*;
    public class Main {
        public static void main(String[] args) {
            List<String> names = Arrays.asList("alice", "bob", "charlie");
            List<String> result = names.stream()
                .map(String::toUpperCase)
                .filter(s -> s.length() > 3)
                .collect(Collectors.toList());
            System.out.println(result);
        }
    }
    • A[ALICE, BOB, CHARLIE]
    • B[BOB]
    • C[ALICE, CHARLIE]
    • D[alice, charlie]
  6. 6.What does flatMap() do differently from map()?

    • AflatMap() is faster than map()
    • BflatMap() only works with Optional
    • CflatMap() applies the function in parallel
    • DflatMap() maps each element to a Stream and then flattens all those streams into one
  7. 7.What does this code print?

    import java.util.*;
    import java.util.stream.*;
    public class Main {
        public static void main(String[] args) {
            List<List<Integer>> nested = Arrays.asList(
                Arrays.asList(1, 2, 3),
                Arrays.asList(4, 5),
                Arrays.asList(6)
            );
            List<Integer> flat = nested.stream()
                .flatMap(Collection::stream)
                .collect(Collectors.toList());
            System.out.println(flat);
        }
    }
    • A[1, 2, 3, 4, 5, 6]
    • B[[1,2,3],[4,5],[6]]
    • CCompilation error
    • D[1, 4, 6]
  8. 8.What does Collectors.groupingBy() do?

    • APartitions the stream into exactly two groups
    • BSorts elements into groups in ascending order
    • CReduces all elements to a single grouped value
    • DGroups stream elements into a Map by a classifier function
  9. 9.What does this code print?

    import java.util.*;
    import java.util.stream.*;
    public class Main {
        public static void main(String[] args) {
            List<String> words = Arrays.asList("cat","car","dog","can","dot");
            Map<Character, List<String>> grouped = words.stream()
                .collect(Collectors.groupingBy(s -> s.charAt(0)));
            System.out.println(grouped.get('c').size());
            System.out.println(grouped.get('d').size());
        }
    }
    • A3 2
    • B3 1
    • C2 3
    • D2 2
  10. 10.What is reduce() used for in streams?

    • AReducing the stream to fewer elements via filtering
    • BReducing memory usage of the stream
    • CRemoving duplicate elements
    • DCombining all elements into a single value using an accumulator function
  11. 11.What does this code print?

    import java.util.*;
    import java.util.stream.*;
    public class Main {
        public static void main(String[] args) {
            List<Integer> nums = Arrays.asList(1, 2, 3, 4, 5);
            int product = nums.stream()
                .reduce(1, (a, b) -> a * b);
            System.out.println(product);
        }
    }
    • A15
    • B120
    • C5
    • D1
  12. 12.What is the difference between findFirst() and findAny()?

    • AThey are identical for sequential streams and parallel streams
    • BfindAny() is always faster than findFirst()
    • CfindFirst() returns the first element in encounter order; findAny() may return any element — useful for parallel streams
    • DfindFirst() returns the largest element; findAny() returns the smallest
  13. 13.What does this code print?

    import java.util.*;
    import java.util.stream.*;
    public class Main {
        public static void main(String[] args) {
            OptionalInt max = IntStream.of(3, 1, 4, 1, 5, 9, 2, 6)
                .filter(n -> n < 6)
                .max();
            System.out.println(max.getAsInt());
        }
    }
    • A5
    • B4
    • C6
    • D9
  14. 14.What is a parallel stream and when should you use one?

    • AA stream that processes elements in alphabetical order
    • BA stream that can be used by multiple threads simultaneously without synchronisation
    • CA stream that splits work across multiple CPU threads using the ForkJoinPool — useful for large data sets with CPU-intensive operations
    • DA stream connected to a parallel database query
  15. 15.What does this code print?

    import java.util.*;
    import java.util.stream.*;
    public class Main {
        public static void main(String[] args) {
            List<String> words = Arrays.asList("hello", "world", "java", "stream");
            String result = words.stream()
                .filter(w -> w.length() > 4)
                .map(String::toUpperCase)
                .sorted()
                .collect(Collectors.joining(", "));
            System.out.println(result);
        }
    }
    • ASTREAM, WORLD
    • BHELLO, STREAM, WORLD
    • CHELLO, JAVA, STREAM
    • DHELLO, WORLD, STREAM
  16. 16.What does Collectors.partitioningBy() return?

    • ATwo separate streams divided by a condition
    • BA List split at a given index
    • CA Map<Boolean, List<T>> with true and false keys separating elements by a predicate
    • DA SortedMap with two partitions
  17. 17.What does this code print?

    import java.util.*;
    import java.util.stream.*;
    public class Main {
        public static void main(String[] args) {
            List<Integer> nums = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
            Map<Boolean, List<Integer>> parts = nums.stream()
                .collect(Collectors.partitioningBy(n -> n % 2 == 0));
            System.out.println(parts.get(true).size());
            System.out.println(parts.get(false).size());
        }
    }
    • A5 5
    • B10 0
    • C6 4
    • D4 6
  18. 18.Can a stream be reused after a terminal operation?

    • AYes, streams can be reused like collections
    • BNo, a stream is consumed after the terminal operation and throws IllegalStateException if reused
    • COnly parallel streams can be reused
    • DYes, but only for read operations like count()
  19. 19.What does this code print?

    import java.util.*;
    import java.util.stream.*;
    public class Main {
        public static void main(String[] args) {
            List<Integer> nums = Arrays.asList(5, 3, 8, 1, 9, 2, 7);
            Optional<Integer> first = nums.stream()
                .sorted()
                .filter(n -> n > 4)
                .findFirst();
            System.out.println(first.orElse(-1));
        }
    }
    • A7
    • B8
    • C5
    • D9
  20. 20.Which of the following is a correct way to sum a list of integers using streams?

    • Alist.stream().collect(Collectors.sum())
    • Blist.stream().mapToInt(Integer::intValue).sum()
    • Clist.stream().reduce(Integer::sum)
    • Dlist.stream().sum()