AlgoMaster Logo

Lambda & Streams - Quiz

Last Updated: December 6, 2025

1 min read

Lambda & Streams Exercises

29 quizzes

1
Code Completion

Use a lambda-compatible method to check if any element in the stream matches the condition.

java
1
boolean hasLongName = names.stream().(name -> name.length() > 10);

Click an option to fill the blank:

2
Code Completion

Use a method reference to sort users by their natural ordering using a Comparator factory.

java
1
users.sort(Comparator.(User::getName));

Click an option to fill the blank:

3
Code Completion

Create an Optional from a value that might be null.

java
1
Optional<String> safeEmail = Optional.(user.getEmail());

Click an option to fill the blank:

4
Multiple Choice

Which statement about lambda expressions in Java is true?

5
Multiple Choice

Which method reference form refers to an instance method of an existing object?

6
Multiple Choice

Which statement about Java Streams is correct?

7
Multiple Choice

Which pair correctly describes intermediate vs terminal operations?

8
Multiple Choice

Which stream operation is a terminal operation?

9
Multiple Choice

Which Collector would you use to group users by their city?

10
Multiple Choice

When might a parallel stream be a bad choice?

11
Multiple Choice

Which Optional method allows you to provide a default value if the Optional is empty?

12
Multiple Choice

What does Stream.flatMap typically help you achieve?

13
Multiple Choice

Which terminal operation would you use to check if no elements in a stream satisfy a condition?

14
Multiple Choice

Which code correctly uses a constructor reference with streams?

15
Sequencing

Order the steps to filter and collect a list of emails from active users using streams.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to safely handle a possibly null address field using Optional.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this lambda-based computation?

1import java.util.List;
2
3public class Test {
4    public static void main(String[] args) {
5        List<Integer> nums = List.of(1, 2, 3, 4);
6        int result = nums.stream()
7                         .filter(n -> n % 2 == 0)
8                         .map(n -> n * n)
9                         .reduce(0, Integer::sum);
10        System.out.println(result);
11    }
12}
18
Output Prediction

What does this method reference and Optional pipeline print?

1import java.util.Optional;
2
3public class Test {
4    public static void main(String[] args) {
5        Optional<String> name = Optional.of("alice");
6        String result = name.map(String::toUpperCase)
7                            .orElse("UNKNOWN");
8        System.out.println(result);
9    }
10}
19
Output Prediction

What single line is printed by this code using Stream.generate and limit?

1import java.util.stream.Stream;
2
3public class Test {
4    public static void main(String[] args) {
5        long count = Stream.generate(() -> "x")
6                           .limit(5)
7                           .count();
8        System.out.println(count);
9    }
10}
20
Output Prediction

What does this Collectors.joining operation print?

1import java.util.List;
2import java.util.stream.Collectors;
3
4public class Test {
5    public static void main(String[] args) {
6        List<String> tags = List.of("java", "streams", "lambda");
7        String joined = tags.stream()
8                            .map(String::toUpperCase)
9                            .collect(Collectors.joining(";"));
10        System.out.println(joined);
11    }
12}
21
Output Prediction

What is printed when using a parallel stream with sum?

1import java.util.stream.IntStream;
2
3public class Test {
4    public static void main(String[] args) {
5        int sum = IntStream.rangeClosed(1, 4)
6                           .parallel()
7                           .sum();
8        System.out.println(sum);
9    }
10}
22
Bug Spotting

Find the bug in this code that maps a list of names to their lengths using method references.

Click on the line(s) that contain the bug.

java
1
import java.util.List;
2
import java.util.stream.Collectors;
3
 
4
public class NameLengths {
5
    public static List<Integer> lengths(List<String> names) {
6
        return names.stream()
7
                    .map(String::new)
8
                    .collect(Collectors.toList());
9
    }
10
}
23
Bug Spotting

Identify the bug in this code that uses Optional to get the first even number.

Click on the line(s) that contain the bug.

java
1
import java.util.List;
2
import java.util.Optional;
3
 
4
public class FirstEven {
5
    public static int firstEvenOrDefault(List<Integer> numbers) {
6
        Optional<Integer> opt = numbers.stream()
7
                                       .filter(n -> n % 2 == 0)
8
                                       .findFirst();
9
        return opt.get();
10
    }
11
}
24
Matching

Match the stream-related concepts with their descriptions.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match the Optional methods with their behavior.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to filter active users and collect their usernames into a List.

java
1
import java.util.List;
2
import java.util.stream.Collectors;
3
4
class User {
5
private boolean active;
6
private String username;
7
public boolean isActive() { return active; }
8
public String getUsername() { return username; }
9
}
10
11
public class Example {
12
public List<String> activeUsernames(List<User> users) {
13
return users.stream()
14
.(User::isActive)
15
.(User::getUsername)
16
.collect(Collectors.toList());
17
}
18
}

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to safely get the uppercased city from a possibly null address.

java
1
import java.util.Optional;
2
3
class Address {
4
private String city;
5
public String getCity() { return city; }
6
}
7
8
public class Example {
9
public String cityOrUnknown(Address address) {
10
return Optional.(address)
11
.(Address::getCity)
12
.map(String::toUpperCase)
13
.orElse("UNKNOWN");
14
}
15
}

Click an option to fill blank 1:

28
Hotspot Selection

Click the line that is most likely to cause a performance issue when used inside a parallel stream pipeline.

Click on the line to select.

java
1
import java.util.List;
2
 
3
public class Demo {
4
    public void logInParallel(List<String> messages) {
5
        messages.parallelStream()
6
                .forEach(msg -> {
7
                    System.out.println(msg);
8
                });
9
    }
10
}
29
Hotspot Selection

Click the line that can throw a NullPointerException when using method references.

Click on the line to select.

java
1
import java.util.List;
2
 
3
public class Demo {
4
    public void printNames(List<String> names) {
5
        names.stream()
6
             .map(String::toUpperCase)
7
             .forEach(System.out::println);
8
    }
9
}

Premium Content

Subscribe to unlock full access to this content and more premium articles.