0
0
Kotlinprogramming~5 mins

Take, drop, and chunked in Kotlin - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
A[1, 2, 3, 4]
B[1, 2]
C[2, 3]
D[3, 4]
What is the output of listOf(1, 2, 3).drop(5)?
A[]
B[1, 2, 3]
C[3]
D[2, 3]
How does chunked(2) split listOf(1, 2, 3, 4, 5)?
A[[1, 2, 3, 4, 5]]
B[[1, 2, 3], [4, 5]]
C[[1], [2], [3], [4], [5]]
D[[1, 2], [3, 4], [5]]
Which function would you use to remove the first 3 elements from a list?
Adrop(3)
Bchunked(3)
Ctake(3)
Dslice(3)
If you want to get the first 4 elements of a list but the list only has 2 elements, what does take(4) return?
AAn error
BThe first 4 elements
CThe whole list
DAn empty list
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.