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.
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());Drag steps to the numbered slots on the right, or tap a step then tap a slot.
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?
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.
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);
```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> 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> 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 IllegalStateExceptionList<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);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));