1Z0-829 Working with Streams and Lambda Expressions Practice Question
Exhibit
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);Refer to the exhibit. What is the output?
⚠ Common exam trap
The trap here is that candidates may miscompute string lengths (e.g., thinking "Charlie" has 5 characters) or confuse the grouping key with the index, leading to incorrect assignment of names to keys.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
{3=[Bob], 5=[Alice, David], 7=[Charlie]}
The code groups names by the length of their string using `Collectors.groupingBy(String::length, Collectors.toList())`. The names "Alice" and "David" have length 5, "Bob" has length 3, and "Charlie" has length 7. The resulting map has keys 3, 5, and 7. Although `groupingBy` returns a `HashMap` without guaranteed order, the expected output in this context displays the keys in ascending numerical order: 3, 5, 7. Therefore, option C correctly shows the groups in that order with the corresponding names. Option B is incorrect because it lists the keys in non-ascending order (5, 3, 7), which does not match the typical output format.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
{3=[Bob], 5=[Alice, David, Charlie]}
Why it's wrong here
Incorrect; Charlie length is 7.
- ✗
{5=[Alice, David], 3=[Bob], 7=[Charlie]}
Why it's wrong here
Order not guaranteed but content is same as A; however, A is more standard.
- ✓
{3=[Bob], 5=[Alice, David], 7=[Charlie]}
Why this is correct
Correct.
- ✗
{3=[Bob], 5=[Alice], 7=[Charlie, David]}
Why it's wrong here
Incorrect; David length is 5, not 7.
Go deeper
Related to this question
About these practice questions
Courseiva writes every 1Z0-829 question from scratch — 513 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This 1Z0-829 practice question is part of Courseiva's free Oracle certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the 1Z0-829 exam.