Recall & Review
beginner
What does the
take(n) function do in Kotlin collections?It returns the first
n elements from a collection. If the collection has fewer than n elements, it returns the whole collection.Click to reveal answer
beginner
Explain the purpose of the
drop(n) function in Kotlin.It returns a list without the first
n elements. If the collection has fewer than n elements, it returns an empty list.Click to reveal answer
beginner
What does the
chunked(size) function do in Kotlin?It splits a collection into smaller lists (chunks) each of the given
size. The last chunk may be smaller if there are not enough elements.Click to reveal answer
beginner
How would you get the first 3 elements of a list
listOf(1, 2, 3, 4, 5) using Kotlin?Use
listOf(1, 2, 3, 4, 5).take(3) which returns [1, 2, 3].Click to reveal answer
beginner
What is the result of
listOf(1, 2, 3, 4, 5).drop(2)?It returns
[3, 4, 5] because it drops the first two elements.Click to reveal answer
What does
listOf(1, 2, 3, 4).take(2) return?✗ Incorrect
The
take(2) function returns the first two elements.What is the output of
listOf(1, 2, 3).drop(5)?✗ Incorrect
Dropping more elements than the list size returns an empty list.
How does
chunked(2) split listOf(1, 2, 3, 4, 5)?✗ Incorrect
The list is split into chunks of size 2; the last chunk has one element.
Which function would you use to remove the first 3 elements from a list?
✗ Incorrect
drop(3) removes the first 3 elements.If you want to get the first 4 elements of a list but the list only has 2 elements, what does
take(4) return?✗ Incorrect
take returns as many elements as possible up to the requested number.Describe how
take, drop, and chunked work on Kotlin collections.Think about how you might split or trim a list like a deck of cards.
You got /3 concepts.
Give an example of when you might use
chunked in a real program.Imagine cutting a big cake into slices to share.
You got /3 concepts.