Search Tutorials


Java Stream API Quiz - Multiple Choice Questions (MCQ) And Answers | JavaInUse

Java Stream API MCQ - Multiple Choice Questions And Answers

Q. What does the Java Stream API provide?

A. Ways to interact with databases
B. Ways to manipulate files
C. Ways to process collections of objects
D. Ways to create graphical interfaces

Q. Which functional interface in Java 8 is used to perform operations on elements in a stream?

A. Consumer
B. Supplier
C. Predicate
D. Function

Q. What does the Stream map() method do in Java?

A. Filters elements based on a condition
B. Transforms elements into a different type
C. Sorts elements in a stream
D. Concatenates elements in a stream

Q. What does the Stream filter() method do in Java?

A. Transforms elements into a different type
B. Sorts elements in a stream
C. Filters elements based on a condition
D. Concatenates elements in a stream

Q. What does the Stream reduce() method do in Java?

A. Combines elements in a stream into a single value
B. Transforms elements into a different type
C. Sorts elements in a stream
D. Filters elements based on a condition

Q. What does the Stream forEach() method do in Java?

A. Applies a function to each element in the stream
B. Transforms elements into a different type
C. Sorts elements in a stream
D. Filters elements based on a condition

Q. What does the Stream sorted() method do in Java?

A. Transforms elements into a different type
B. Filters elements based on a condition
C. Sorts elements in a stream
D. Concatenates elements in a stream

Q. What does the Stream distinct() method do in Java?

A. Transforms elements into a different type
B. Filters elements based on a condition
C. Removes duplicate elements from a stream
D. Sorts elements in a stream

Q. What is the purpose of the Predicate functional interface in Java 8?

A. To produce results
B. To consume values
C. To filter elements based on a condition
D. To transform values

Q. What does the Stream limit() method do in Java?

A. Transforms elements into a different type
B. Filters elements based on a condition
C. Sorts elements in a stream
D. Limits the number of elements in a stream

Q. What does the map method do in Java Stream API?

A.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> squaredNumbers = numbers.stream()
                                         .map(num -> num * num)
                                         .collect(Collectors.toList());
    
B.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> squaredNumbers = numbers.stream()
                                         .filter(num -> num % 2 == 0)
                                         .collect(Collectors.toList());
    
C.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> squaredNumbers = numbers.stream()
                                         .reduce(1, (a, b) -> a * b);
    
D.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> squaredNumbers = numbers.stream()
                                         .sorted()
                                         .collect(Collectors.toList());
    

Q. What will be the output of the following code snippet?

    List<String> names = Arrays.asList("John", "Doe", "Jane", "Smith");

    String concatenatedString = names.stream()
                                     .reduce("", (a, b) -> a + b);
                                    
    System.out.println(concatenatedString);
    
A. JohnDoeJaneSmith B. John Doe Jane Smith C. Jane Smith John Doe D. NoSuchElement Exception

Q. Which terminal operation should be used to collect elements of a stream into a collection in Java Stream API?

A.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> collectedNumbers = numbers.stream()
                                           .collect(Collectors.toList());
    
B.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    Integer sum = numbers.stream()
                        .reduce(0, (a, b) -> a + b);
    
C.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    long count = numbers.stream()
                        .count();
    
D.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    Optional<Integer> max = numbers.stream()
                                  .max(Integer::compare);
    





Q. What does the distinct method do in Java Stream API?

A.
    List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5);
    List<Integer> distinctNumbers = numbers.stream()
                                           .distinct()
                                           .collect(Collectors.toList());
    
B.
    List<String> names = Arrays.asList("John", "Doe", "Jane", "Smith");
    List<String> distinctNames = names.stream()
                                      .distinct()
                                      .collect(Collectors.toList());
    
C.
    List<String> names = Arrays.asList("John", "Doe", "Jane", "Smith");
    List<String> distinctNames = names.stream()
                                      .filter(name -> name.length() > 4)
                                      .collect(Collectors.toList());
    
D.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    List<Integer> distinctNumbers = numbers.stream()
                                           .sorted()
                                           .collect(Collectors.toList());
    

Q. What is the purpose of the reduce method in Java Stream API?

A.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    Integer sum = numbers.stream()
                        .reduce(0, (a, b) -> a + b);
    
B.
    List<String> names = Arrays.asList("John", "Doe", "Jane", "Smith");
    String concatenatedString = names.stream()
                                     .reduce("", (a, b) -> a + b);
    
C.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    long count = numbers.stream()
                        .count();
    
D.
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    Optional<Integer> max = numbers.stream()
                                  .max(Integer::compare);