Recall & Review
beginner
What does the
partition function do in Kotlin?It splits a collection into two lists based on a condition: one list with elements that satisfy the condition, and another with elements that don't.
Click to reveal answer
beginner
How do you use
partition to separate even and odd numbers from a list?Use
val (evens, odds) = list.partition { it % 2 == 0 }. This creates two lists: evens with even numbers and odds with odd numbers.Click to reveal answer
intermediate
What is the return type of the
partition function?It returns a
Pair<List<T>, List<T>>, where the first list contains elements matching the predicate, and the second list contains the rest.Click to reveal answer
beginner
Can
partition be used on any Kotlin collection?Yes,
partition works on any Iterable, like lists and sets.Click to reveal answer
intermediate
What happens if no elements satisfy the condition in
partition?The first list in the returned pair will be empty, and the second list will contain all elements.
Click to reveal answer
What does Kotlin's
partition function return?✗ Incorrect
partition returns a Pair of two lists: one with elements that satisfy the condition, and one with those that don't.
How do you capture the two lists returned by
partition?✗ Incorrect
You use destructuring to get both lists from the Pair returned by partition.
If you want to split a list of numbers into positives and non-positives, which predicate fits
partition?✗ Incorrect
The predicate { it > 0 } selects positive numbers for the first list.
What type of collections can use
partition in Kotlin?✗ Incorrect
partition works on any Iterable, including lists and sets.
What will be the size of the first list if no elements satisfy the
partition condition?✗ Incorrect
If no elements match, the first list is empty.
Explain how Kotlin's
partition function works and give a simple example.Think about separating items based on a yes/no question.
You got /4 concepts.
Describe the return type of
partition and how to access the two lists it produces.Remember how Kotlin lets you unpack pairs easily.
You got /4 concepts.