Java Streams API Quiz - MCQ - Multiple Choice Questions

This post contains a few useful Java Streams API multiple-choice questions to self-test your knowledge on Java 8 streams API.

The answer and explanation have been given for each MCQ.

1. Choose the best option based on this program:

import java.util.stream.Stream;
public class AllMatch {
    public static void main(String[] args) {
        boolean result = Stream.of("do", "re", "mi", "fa", "so", "la", "ti")
            .filter(str - > str.length() > 5) // #1
            .peek(System.out::println) // #2
            .allMatch(str - > str.length() > 5); // #3
        System.out.println(result);
    }
}
A. This program results in a compiler error in the line marked with comment #1 

B. This program results in a compiler error in line marked with comment #2 

C. This program results in a compiler error in line marked with comment #3 

D. This program prints: false 

E. This program prints the strings “do”, “re”, “mi”, “fa”, “so”, “la”, “ti”, and “false” in separate lines 

F. This program prints: true

Answer

F. This program prints: true

Explanation 

The predicate str -> str.length() > 5 returns false for all the elements because the length of each string is 2. Hence, the filter() method results in an empty stream and the peek() method does not print anything.

The allMatch() method returns true for an empty stream and does not evaluate the given predicate. Hence this program prints true.

2. Which of the following are intermediate operations?

A. limit

B. peek

C. anyMatch

D. skip

Answer

The correct answers are A, B, and D.

Explanation 

limit, peek, and skip are intermediate operations. The anyMatch is a terminal operation.

3. Which of the following are terminal operations?

A. sorted

B. flatMap

C. max

D. distinct

Answer

The correct answer is C.

Explanation 

max is the only terminal operation. sorted, flatMap, and distinct are intermediate operations.

4. Which of the following are short-circuit operations?

A. reduce

B. parallel

C. findNone

D. findFirst

Answer

The correct answer is D.

Explanation 

findFirst is the only short-circuit operation. reduce is a terminal operation. the parallel is an intermediate operation. findNone doesn't exist.

5. Given

public class Question {
    public static void main(String[] args) {
        IntStream.range(1, 10)
            .filter(i -> {
                System.out.print("1");
                return i % 2 == 0;
            })
            .filter(i -> {
                System.out.print("0");
                return i > 3;
            })
            .limit(1)
            .forEach(i -> {
                System.out.print(i);
            });
    }
}
What is the result?

A. 101010104

B. 1111111110000000004

C. 11041106

D. 1101104

E. An exception is thrown

Answer

The correct answer is D.

Explanation 

IntStream.range(1, 10) produces a stream of ints from 1 to 9. Since the limit is a short-circuit operation, when there's an element that fulfills both filter conditions, the stream will terminate.

When 1 is evaluated, 1 is printed in the first filter, but since false is returned, 2 is processed. This element passes the first filter, but not the second one, so 10 is printed. 

When 3 is processed, 1 from the first filter is printed and then 4 is processed. This element passes both conditions so 10 is printed and the stream finishes, printing 4 at the end because of the forEach operation.

6. Choose the best option based on this program:

import java.util.Optional;
import java.util.stream.Stream;
public class StringToUpper {
    public static void main(String args[]) {
        Stream.of("eeny ", "meeny ", null).forEach(StringToUpper::toUpper);
    }
    private static void toUpper(String str) {
        Optional < String > string = Optional.ofNullable(str);
        System.out.print(string.map(String::toUpperCase).orElse("dummy"));
    }
}
A. This program prints: EENY MEENY dummy 

B. This program prints: EENY MEENY DUMMY 

C. This program prints: EENY MEENY null 

D. This program prints: Optional[EENY] Optional[MEENY] Optional[dummy] 

E. This program prints: Optional[EENY] Optional[MEENY] Optional[DUMMY]

Answer

A. This program prints: EENY MEENY dummy

Explanation 

Note that the variable string points to Optional.ofNullable(str). When the element null is encountered in the stream, it cannot be converted to uppercase, and hence the orElse() method executes to return the string “dummy”. 

In this program, if Optional.of(str) were used instead of Optional.ofNullable(str) the program would have resulted in throwing a NullPointerException.

7. What is the output of this program?

import java.util.stream.*;
public class Question {
    public static void main(String[] args) {
        IntStream.of(1, 1, 3, 3, 7, 6, 7)
            .distinct()
            .parallel()
            .map(i -> i*2)
            .sequential()
            .forEach(System.out::print);
    }
}
A. It can print 142612 sometimes

B. 1133767

C. It can print 3131677 sometimes

D. 261412

Answer

The correct answer is D.

Explanation 

The call of sequential() turns the whole stream pipeline into a sequential one. In general, we can say that the last call to parallel() or sequential() is the one that affects the whole stream pipeline.

Related Posts

  1. Java String Quiz
  2. Java Arrays Quiz
  3. Java Loops Quiz
  4. Java OOPS Quiz
  5. Java OOPS Quiz - Part 1
  6. Java OOPS Quiz - Part 2
  7. Java Exception Handling Quiz
  8. Java Collections Quiz
  9. Java Generics Quiz
  10. Java Multithreading Quiz
  11. JDBC Quiz
  12. Java Lambda Expressions Quiz
  13. Java Functional Interfaces Quiz
  14. Java Streams API Quiz
  15. Java Date Time Quiz
  16. Java 8 Quiz

Free Spring Boot Tutorial - 5 Hours Full Course


Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course