1Z0-829 Working with Streams and Lambda Expressions • Complete Question Bank
Complete 1Z0-829 Working with Streams and Lambda Expressions question bank — all 0 questions with answers and detailed explanations.
Drag or tap steps into the slots.
Drag a concept onto its matching description — or click a concept then click the description.
Checked exception
Unchecked exception
Checked exception
Unchecked exception
Checked exception
Given a list of integers, a developer wants to compute the sum of squares of numbers greater than 10. The following code is written:
int sum = list.stream().filter(i -> i>10).mapToInt(i->i*i).sum();
But the sum is incorrect. What is the most likely reason?
Refer to the exhibit.
List<String> list = List.of("a", "b", "c");
Stream<String> stream = list.stream();
stream.filter(s -> s.startsWith("b")).forEach(System.out::print);
stream.forEach(System.out::print);Refer to the exhibit.
```java
List<Integer> numbers = Arrays.asList(2, 3, 4, 5);
int result = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(n -> n * 3)
.sum();
System.out.println(result);
```List<String> list = Arrays.asList("a", "b", "c");
Stream<String> stream = list.stream();
stream.forEach(System.out::print);
stream.forEach(System.out::print);List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(n -> n * 2)
.sum();
System.out.println(sum);A company runs a Java 17 microservice that processes real-time financial transactions. The application receives a large number of transactions per second, each with a timestamp, amount, and type. The current implementation uses a sequential stream to filter and aggregate transactions into a Map<TransactionType, DoubleSummaryStatistics>. The team observes high latency and CPU spikes during peak loads. They suspect the stream pipeline is inefficient. The pipeline code is:
Map<TransactionType, DoubleSummaryStatistics> stats = transactions.stream() .filter(t -> t.getTimestamp().isAfter(Instant.now().minusSeconds(60))) .collect(Collectors.groupingBy(Transaction::getType, Collectors.summarizingDouble(Transaction::getAmount)));
The transactions list is an ArrayList that is frequently modified by other threads (adding new transactions). The system has multiple CPU cores available. Which of the following changes is the MOST effective way to improve performance while maintaining correctness?
List<Integer> numbers = Arrays.asList(1,2,3,4,5);
long count = numbers.stream().filter(n -> n%2==0).count();
System.out.println("Count: " + count);List<String> items = List.of("apple", "banana", "apple", "orange", "banana", "apple");
Map<String, Long> countMap = items.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
System.out.println(countMap);List<Integer> numbers = List.of(10, 20, 30, 40, 50);
Optional<Integer> result = numbers.stream()
.filter(n -> n > 100)
.findFirst();
System.out.println(result.orElse(-1));List<Student> students = Arrays.asList(
new Student("Alice", 85),
new Student("Bob", 92),
new Student("Charlie", 78),
new Student("David", 88)
);
OptionalDouble avg = students.stream()
.filter(s -> s.getScore() > 80)
.mapToInt(Student::getScore)
.average();
System.out.println(avg.orElse(0.0));import java.util.stream.Collectors;
import java.util.List;
List<String> words = List.of("apple", "banana", "cherry", "date");
String result = words.stream()
.filter(w -> w.length() > 4)
.collect(Collectors.joining(", "));
System.out.println(result);Map<String, Integer> scores = new HashMap<>();
scores.put("Alice", 90);
scores.put("Bob", 85);
scores.put("Charlie", 95);
int sum = scores.entrySet().stream()
.filter(e -> e.getValue() >= 90)
.mapToInt(e -> e.getValue())
.sum();
System.out.println(sum);Stream<String> stream = Stream.of("one", "two", "three", "four");
List<String> list = stream
.filter(s -> s.startsWith("t"))
.collect(Collectors.toList());
System.out.println(list);List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int result = numbers.stream()
.reduce(0, (a, b) -> a - b);
System.out.println(result);A developer writes the following code using the Stream API:
List<String> list = List.of("a", "b", "c"); String result = list.stream().reduce("", (s1, s2) -> s1 + s2); System.out.println(result);
What is the output?
A developer writes:
List<Integer> list = List.of(1, 2, 3); Optional<Integer> opt = list.stream().reduce((a, b) -> a + b); System.out.println(opt.get());
What is the result?
Given:
List<String> words = Arrays.asList("apple", "banana", "cherry"); Map<Integer, List<String>> map = words.stream().collect(Collectors.groupingBy(String::length)); System.out.println(map);
What is the output?
What does the following code print?
List<Integer> list = List.of(1, 2, 3, 4, 5); list.stream().filter(i -> i % 2 == 0).forEach(System.out::print);
Refer to the exhibit.
Error log:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method flatMap(Function<? super String,? extends Stream<? extends String>>) in the type Stream<String> is not applicable for the arguments (String::chars)
Code snippet:
List<String> words = List.of("hello", "world");
List<Character> letters = words.stream()
.flatMap(String::chars)
.mapToObj(c -> (char) c)
.collect(Collectors.toList());Given: List<String> words = List.of("apple", "banana", "cherry");
var result = words.stream().filter(s -> s.length() > 5).collect(Collectors.toUnmodifiableList());
What happens if another thread tries to add to result?
Refer to the exhibit.
var result = Stream.of("1", "2", "3")
.map(Integer::parseInt)
.collect(Collectors.teeing(
Collectors.summingInt(i -> i),
Collectors.counting(),
(sum, count) -> sum / count
));
System.out.println(result);Refer to the exhibit.
```java
List<Integer> source = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
List<Integer> results = new ArrayList<>();
source.parallelStream()
.map(n -> { results.add(n); return n * 2; })
.forEachOrdered(System.out::println);
```List<String> words = Arrays.asList("apple", "banana", "cherry", "date", "elderberry");
Stream<String> stream = words.stream();
stream.filter(w -> w.startsWith("a"))
.map(String::toUpperCase)
.forEach(System.out::println);
// Output: APPLE
// Then:
stream.forEach(System.out::println); // Throws IllegalStateExceptionRefer to the exhibit. What is the output?
```java
import java.util.stream.IntStream;
public class Example {
public static void main(String[] args) {
int sum = IntStream.rangeClosed(0, 5).filter(i -> i >= 0) .sum(); System.out.println(sum);
} }
```
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
int sum = numbers.stream()
.reduce(0, (a, b) -> a + b);
System.out.println(sum);List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
Map<Integer, List<String>> grouped = names.stream()
.collect(Collectors.groupingBy(String::length));
System.out.println(grouped);A developer writes the following code to partition a list of integers into even and odd numbers, then print the count of each group:
import java.util.*;
import java.util.stream.*;
public class PartitionExample {
public static void main(String[] args) {List<Integer> numbers = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); Map<Boolean, List<Integer>> partitioned = numbers.stream() .collect(Collectors.partitioningBy(n -> n % 2 == 0)); System.out.println(partitioned.get(true).size() + " " + partitioned.get(false).size());
} }
What is the output?
A logistics company uses a Stream<String> of shipment tracking codes. A developer writes:
Stream<String> codes = Stream.of("AB12", "CD34", "EF56"); Map<Boolean, List<String>> result = codes.collect(Collectors.partitioningBy(c -> c.startsWith("A")));
What is the value of result.get(false) after execution?
A financial application processes a Stream<Integer> of transaction amounts. A developer writes:
Optional<Integer> total = amounts.reduce((a, b) -> a + b);
If the stream is empty, what happens when the developer calls total.orElseThrow()?
A developer is writing a method that takes a List<String> named names and must return a single String consisting of the names joined by a comma, where null elements are replaced with the literal text "UNKNOWN". The developer writes:
String result = names.stream() .map(n -> n == null ? "UNKNOWN" : n) .collect(Collectors.joining(","));
Which statement about this code is true?
A developer is writing a method that groups employees by department and then, for each department, computes the total salary. The code is:
Map<String, Double> totals = employees.stream() .collect(Collectors.groupingBy(Employee::getDepartment, Collectors.summingDouble(Employee::getSalary)));
Which statement best describes the type of the values in the resulting map and why?
A developer has a Stream<String> named words and writes the following pipeline:
String out = words .filter(w -> w.length() > 3) .findFirst() .orElse("none");
Later the same Stream reference words is passed to a second pipeline. What is the result?
A developer writes the following pipeline to find the first even number greater than 100 in an infinite stream:
Optional<Integer> result = Stream.iterate(1, n -> n + 1) .filter(n -> n % 2 == 0 && n > 100) .findFirst();
What happens when this code runs?
A developer has a Stream<String> of city names and writes the following pipeline:
long count = cities.filter(c -> c.length() > 5).count();
Which statement best describes the nature of the count() operation in this pipeline?
A developer needs to concatenate a stream of strings into a single comma-separated string, but also wants to prefix the result with '[' and suffix it with ']'. The code is:
String result = Stream.of("a", "b", "c") .collect(Collectors.joining(", ", "[", "]"));
What is the value of result?
A developer needs to count how many elements in a Stream<Integer> named nums are greater than 10 and also compute the sum of those same elements. The developer writes:
long count = nums.filter(n -> n > 10).count();
int sum = nums.filter(n -> n > 10).mapToInt(Integer::intValue).sum();
What happens when this code runs?
A developer is using a parallel stream to compute the sum of squares of a large list of integers:
int sum = list.parallelStream()
.mapToInt(n -> n * n) .sum();
The developer notices that the result is sometimes incorrect when the list contains null values. Which statement best explains the behavior?
A developer processes a Stream<String> named codes and wants to remove duplicates while preserving the order in which codes first appear. The developer writes:
List<String> result = codes.distinct().collect(Collectors.toList());
Which statement about this code is true?