It groups elements of a collection into a map, where each key is a category and the value is a list of elements belonging to that category.
Click to reveal answer
beginner
How do you specify the category for grouping when using groupBy?
You provide a lambda function that returns the key for each element, which determines the category it belongs to.
Click to reveal answer
beginner
Example: What is the result of listOf(1, 2, 3, 4).groupBy { it % 2 }?
A map grouping numbers by even or odd: {0=[2, 4], 1=[1, 3]} where 0 means even and 1 means odd.
Click to reveal answer
intermediate
Can groupBy be used with custom objects?
Yes, you can group custom objects by any property or computed value by providing a lambda that returns the grouping key.
Click to reveal answer
intermediate
What type does groupBy return?
It returns a Map<K, List<T>> where K is the key type from the lambda and T is the element type.
Click to reveal answer
What is the output type of groupBy in Kotlin?
AMap<Key, List<Element>>
BList<Key>
CSet<Element>
DArray<Element>
✗ Incorrect
groupBy returns a map where each key maps to a list of elements in that group.
Which lambda expression groups a list of strings by their first letter?
Alist.groupBy { it.first() }
Blist.groupBy { it.length }
Clist.groupBy { it.toUpperCase() }
Dlist.groupBy { it.last() }
✗ Incorrect
Grouping by the first letter uses it.first() in the lambda.
If you want to group numbers by whether they are positive or negative, which key selector is best?
AgroupBy { it % 2 }
BgroupBy { it == 0 }
CgroupBy { it.toString() }
DgroupBy { it > 0 }
✗ Incorrect
it > 0 returns true or false, grouping numbers by positive or not.
Can groupBy be used on an empty list?
ANo, it throws an exception.
BYes, it returns an empty map.
CYes, it returns null.
DNo, it returns an empty list.
✗ Incorrect
Grouping an empty list returns an empty map without error.
What happens if multiple elements have the same key in groupBy?
AAn error is thrown.
BOnly the first element is kept.
CThey are collected into the list for that key.
DOnly the last element is kept.
✗ Incorrect
All elements with the same key are grouped together in a list.
Explain how the groupBy function works in Kotlin and give a simple example.
Think about sorting items into labeled boxes based on a rule.
You got /4 concepts.
Describe a real-life scenario where you could use groupBy to organize data.
Imagine sorting your friends by their favorite color.
You got /4 concepts.
Practice
(1/5)
1. What does the groupBy function do in Kotlin?
easy
A. It sorts a list in ascending order.
B. It organizes a list into a map based on a key selector function.
C. It removes duplicate elements from a list.
D. It converts a list into a set.
Solution
Step 1: Understand the purpose of groupBy
The groupBy function takes a list and groups its elements by a key, returning a map.
Step 2: Compare with other functions
Sorting, removing duplicates, and converting to set do different things than grouping by a key.
Final Answer:
It organizes a list into a map based on a key selector function. -> Option B
Quick Check:
groupBy = organize by key [OK]
Hint: Remember: groupBy creates categories as map keys [OK]
Common Mistakes:
Confusing groupBy with sorting functions
Thinking groupBy removes duplicates
Mixing groupBy with set conversion
2. Which of the following is the correct syntax to group a list of strings by their first character in Kotlin?
easy
A. val result = list.groupBy { it.first() }
B. val result = list.groupBy(it -> it.first())
C. val result = list.groupBy { it.first }
D. val result = list.groupBy { first(it) }
Solution
Step 1: Review Kotlin lambda syntax
Kotlin uses curly braces with it for single-parameter lambdas: { it.first() }.
Step 2: Check each option
val result = list.groupBy { it.first() } uses correct Kotlin syntax. val result = list.groupBy(it -> it.first()) uses arrow syntax which is invalid here. val result = list.groupBy { it.first } omits parentheses on first() method call, causing compile error. val result = list.groupBy { first(it) } calls undefined function first().
Final Answer:
val result = list.groupBy { it.first() } -> Option A
Quick Check:
Use curly braces and it.first() [OK]
Hint: Use curly braces and 'it' for lambda in groupBy [OK]
Common Mistakes:
Using arrow syntax inside groupBy lambda
Calling undefined functions like first(it)
Omitting parentheses on first() method call
3. What is the output of the following Kotlin code?
val numbers = listOf(1, 2, 3, 4, 5, 6)
val grouped = numbers.groupBy { if (it % 2 == 0) "even" else "odd" }
println(grouped)
medium
A. {odd=[1, 3, 5], even=[2, 4, 6]}
B. {1=[1], 2=[2], 3=[3], 4=[4], 5=[5], 6=[6]}
C. {true=[2, 4, 6], false=[1, 3, 5]}
D. SyntaxError
Solution
Step 1: Understand the grouping condition
The lambda groups numbers by "even" if divisible by 2, else "odd".
Step 2: Group numbers accordingly
Numbers 2,4,6 go to "even"; 1,3,5 go to "odd".
Final Answer:
{odd=[1, 3, 5], even=[2, 4, 6]} -> Option A
Quick Check:
Group by even/odd labels [OK]
Hint: Check grouping keys carefully for expected output [OK]
Common Mistakes:
Confusing keys with values in output
Expecting boolean keys instead of strings
Thinking groupBy sorts the list
4. Identify the error in this Kotlin code snippet:
val words = listOf("apple", "banana", "apricot")
val grouped = words.groupBy { it[0] }
println(grouped)
medium
A. Indexing with it[0] is invalid for strings
B. Must convert characters to strings before grouping
C. groupBy requires a function, not a lambda
D. No error; code runs and groups by first character
Solution
Step 1: Check indexing on strings
In Kotlin, strings support indexing with [0] to get first character.
Step 2: Validate groupBy usage
The lambda { it[0] } is valid and groups words by their first character.
Final Answer:
No error; code runs and groups by first character -> Option D
Quick Check:
Strings support indexing; groupBy accepts lambdas [OK]
Hint: Remember strings can be indexed like arrays in Kotlin [OK]
Common Mistakes:
Thinking indexing strings causes error
Believing groupBy needs named functions only
Assuming characters must be converted to strings
5. Given a list of people represented as pairs Pair<String, Int> where the first element is the name and the second is the age, how would you group them by age category "minor" (age < 18) or "adult" (age >= 18) using groupBy?
hard
A. people.groupBy { it.first }
B. people.groupBy { it.first.length }
C. people.groupBy { if (it.second < 18) "minor" else "adult" }
D. people.groupBy { it.second }
Solution
Step 1: Identify grouping key based on age
We want to group by "minor" if age < 18, else "adult".
Step 2: Use groupBy with conditional key
Access age with it.second and apply if-else to return category string.
Final Answer:
people.groupBy { if (it.second < 18) "minor" else "adult" } -> Option C
Quick Check:
Group by age category using conditional [OK]
Hint: Use if-else inside groupBy lambda for categories [OK]
Common Mistakes:
Grouping by name or name length instead of age
Grouping by exact age instead of categories
Confusing first and second in Pair
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.