Recall & Review
beginner
What does the
zip function do in Kotlin?The
zip function combines two collections into a single list of pairs, where each pair contains elements from the same position in both collections.Click to reveal answer
beginner
How does Kotlin handle collections of different sizes when using
zip?Kotlin stops zipping when the shortest collection ends, so the resulting list has the size of the smaller collection.
Click to reveal answer
beginner
Show a simple example of using
zip with two lists in Kotlin.Example:<br>
val numbers = listOf(1, 2, 3)
val words = listOf("one", "two", "three")
val zipped = numbers.zip(words)
// zipped = [(1, "one"), (2, "two"), (3, "three")]
Click to reveal answer
intermediate
Can you customize the output of
zip in Kotlin?Yes, you can provide a transform function to
zip that defines how to combine elements instead of returning pairs.Click to reveal answer
intermediate
What is the output of this Kotlin code?<br>
val a = listOf(1, 2)
val b = listOf("A", "B", "C")
val result = a.zip(b) { num, str -> "$num$str" }The output is a list: ["1A", "2B"]. The zip stops at the shortest list length (2), and combines elements using the transform function.
Click to reveal answer
What does Kotlin's
zip function return when combining two lists?✗ Incorrect
The
zip function pairs elements from both lists into a list of pairs.If one list has 3 elements and the other has 5, how many pairs does
zip produce?✗ Incorrect
Zipping stops at the shortest list length, so it produces 3 pairs.
How can you customize the output of
zip in Kotlin?✗ Incorrect
You can provide a transform function to
zip to define how elements combine.What is the type of the result when you call
zip without a transform function?✗ Incorrect
Without a transform,
zip returns a list of pairs combining elements.Which of these is a valid use of
zip in Kotlin?✗ Incorrect
Option B uses
zip with a transform function correctly.Explain how the
zip function works in Kotlin and what happens when the collections have different sizes.Think about pairing elements from two lists like matching socks.
You got /3 concepts.
Describe how to customize the output of
zip in Kotlin using a transform function.Imagine mixing ingredients to create a new recipe instead of just pairing them.
You got /3 concepts.