Java Streams Coding Questions and Answers

Welcome to the Java Streams API Coding Quiz. In this quiz, we present 10 coding MCQ questions to test your coding knowledge of the Java Streams API. Each question has a correct and brief explanation.

1. What is the output of the following Java code snippet?

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, (a, b) -> a + b);
System.out.println(sum);
a) 15
b) 10
c) 5
d) 20

Answer:

a) 15

Explanation:

The reduce method performs a reduction on the elements of the stream with the provided identity value and accumulator function, which in this case sums up the numbers.

2. What does this Java code snippet output?

Stream<String> stream = Stream.of("Java", "Python", "C++");
long count = stream.filter(s -> s.startsWith("J")).count();
System.out.println(count);
a) 1
b) 2
c) 3
d) 0

Answer:

a) 1

Explanation:

The filter method filters the stream based on the given predicate. Only "Java" starts with "J", so the count is 1.

3. Identify the output of the following code:

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
String result = names.stream().map(String::toUpperCase).collect(Collectors.joining(", "));
System.out.println(result);
a) ALICE, BOB, CHARLIE
b) Alice, Bob, Charlie
c) ALICE BOB CHARLIE
d) Alice Bob Charlie

Answer:

a) ALICE, BOB, CHARLIE

Explanation:

The map method converts each element to upper case, and collect with Collectors.joining concatenates them with ", " as the delimiter.

4. What will be printed by this Java code?

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> max = numbers.stream().max(Integer::compare);
max.ifPresent(System.out::println);
a) 1
b) 2
c) 3
d) 5

Answer:

d) 5

Explanation:

The max method finds the maximum element based on the given comparator. The maximum value in the list is 5.

5. What does this code snippet output?

List<String> list = Arrays.asList("a", "b", "c", "d", "e");
list.stream().limit(3).forEach(System.out::print);
a) abc
b) abcd
c) abcde
d) a

Answer:

a) abc

Explanation:

The limit method limits the stream to the first 3 elements, so it prints "a", "b", and "c".

6. What is the result of executing this code?

List<String> words = Arrays.asList("hello", "world");
List<String> uniqueLetters = words.stream()
    .flatMap(word -> Arrays.stream(word.split("")))
    .distinct()
    .collect(Collectors.toList());
System.out.println(uniqueLetters);
a) [h, e, l, o, w, r, d]
b) [hello, world]
c) [h, e, l, o, w, o, r, l, d]
d) [h, e, l, l, o, w, o, r, l, d]

Answer:

a) [h, e, l, o, w, r, d]

Explanation:

flatMap breaks each word into individual letters, distinct removes duplicates, and collect gathers the result into a list.

7. What will the following Java code snippet output?

IntStream.range(1, 4).forEach(System.out::print);
a) 123
b) 1234
c) 234
d) 134

Answer:

a) 123

Explanation:

IntStream.range(1, 4) generates a sequential stream from 1 (inclusive) to 4 (exclusive), so it prints 1, 2, 3.

8. What does the following code snippet print?

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
numbers.stream().filter(n -> n % 2 == 0).forEach(System.out::print);
a) 246
b) 135
c) 123456
d) 24

Answer:

a) 246

Explanation:

The filter method filters elements that are even, and forEach prints each remaining element.

9. Determine the output of this Java code:

List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
boolean allMatch = names.stream().allMatch(name -> name.length() > 3);
System.out.println(allMatch);
a) true
b) false
c) Compilation error
d) Runtime exception

Answer:

a) true

Explanation:

The allMatch method checks whether all elements in the stream match the given predicate. Here, all names have more than 3 characters, so it returns true.

10. What is the result of the following code snippet?

List<String> fruits = Arrays.asList("apple", "banana", "cherry", "date");
String result = fruits.stream()
    .filter(fruit -> fruit.contains("a"))
    .sorted()
    .findFirst()
    .orElse("None");
System.out.println(result);
a) apple
b) banana
c) cherry
d) date

Answer:

a) apple

Explanation:

The stream is filtered to include only fruits containing "a", sorted in a natural order, and findFirst gets the first element in the sorted stream. "apple" is the first alphabetically.

Comments