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:
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?
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?
AA list of elements only from the longer list
BA single list containing all elements from both lists
CA map with keys from the first list and values from the second
DA list of pairs combining elements from both 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?
A5
B8
C3
D0
✗ Incorrect
Zipping stops at the shortest list length, so it produces 3 pairs.
How can you customize the output of zip in Kotlin?
ABy passing a transform function to combine elements
BBy changing the list sizes before zipping
CBy using <code>map</code> after <code>zip</code>
DBy converting lists to arrays first
✗ 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?
AList<Pair<T, R>>
BList<T>
CList<R>
DMap<T, R>
✗ Incorrect
Without a transform, zip returns a list of pairs combining elements.
Which of these is a valid use of zip in Kotlin?
Aval zipped = zip(list1, list2)
Bval zipped = list1.zip(list2) { a, b -> a + b }
Cval zipped = list1.zip()
Dval zipped = list1.zip(list2).toMap()
✗ 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.
Practice
(1/5)
1. What does the zip function do when used with two lists in Kotlin?
easy
A. Combines all elements from both lists into one longer list.
B. Pairs elements from both lists into a list of pairs, stopping at the shorter list length.
C. Creates a map from the first list's elements to the second list's elements without pairing.
D. Filters elements that are common to both lists.
Solution
Step 1: Understand the zip function behavior
The zip function pairs elements from two collections by their positions.
Step 2: Consider the length of the resulting list
The resulting list length equals the smaller of the two input lists, pairing elements until one list runs out.
Final Answer:
Pairs elements from both lists into a list of pairs, stopping at the shorter list length. -> Option B
Quick Check:
Zip pairs elements up to shortest list length [OK]
Hint: Zip pairs elements by index, stops at shortest list length [OK]
Common Mistakes:
Thinking zip concatenates lists instead of pairing
Assuming zip returns a map automatically
Believing zip pairs all elements ignoring list length
2. Which of the following is the correct syntax to zip two lists listA and listB in Kotlin?
easy
A. val zipped = listA + listB.zip()
B. val zipped = zip(listA, listB)
C. val zipped = listA.zip(listB)
D. val zipped = listA.zipWith(listB)
Solution
Step 1: Recall Kotlin's zip syntax
The zip function is called as an extension on the first list, passing the second list as argument.
Step 2: Check each option
val zipped = listA.zip(listB) matches the correct syntax: listA.zip(listB). Others are invalid or incorrect method calls.
Final Answer:
val zipped = listA.zip(listB) -> Option C
Quick Check:
Use listA.zip(listB) to zip two lists [OK]
Hint: Use listA.zip(listB) to combine two lists [OK]
Common Mistakes:
Trying to call zip as a standalone function
Using incorrect method names like zipWith
Concatenating lists instead of zipping
3. What is the output of the following Kotlin code?
val nums = listOf(1, 2, 3)
val chars = listOf('a', 'b')
val zipped = nums.zip(chars)
println(zipped)
medium
A. [(1, a), (2, b), (3, null)]
B. [(a, 1), (b, 2)]
C. [(1, a), (2, b), (3, c)]
D. [(1, a), (2, b)]
Solution
Step 1: Identify the input lists and their lengths
nums has 3 elements, chars has 2 elements.
Step 2: Understand zip output length and pairing
Zip pairs elements by index until the shorter list ends, so only first 2 pairs: (1, 'a') and (2, 'b').
Final Answer:
[(1, a), (2, b)] -> Option D
Quick Check:
Zip length = shorter list length = 2 [OK]
Hint: Zip stops at shorter list length, pairs elements by index [OK]
Common Mistakes:
Expecting zip to fill missing elements with null
Assuming zip pairs all elements ignoring length
Mixing order of elements in pairs
4. The following Kotlin code throws an error. What is the problem?
val list1 = listOf(1, 2, 3)
val list2 = listOf("a", "b", "c")
val zipped = list1.zip(list2) { a, b -> a + b }
println(zipped)
medium
A. Cannot add Int and String directly with + operator.
B. zip requires lists to be the same size.
C. The lambda syntax for zip is incorrect.
D. listOf cannot contain different types.
Solution
Step 1: Analyze the lambda inside zip
The lambda tries to add a (Int) and b (String) using +.
Step 2: Understand Kotlin type rules
Adding Int and String directly with + is not allowed; it causes a type error.
Final Answer:
Cannot add Int and String directly with + operator. -> Option A
Quick Check:
Int + String causes type error in Kotlin [OK]
Hint: Int + String needs explicit conversion or concatenation [OK]
Common Mistakes:
Assuming zip needs equal list sizes
Thinking lambda syntax is wrong
Believing listOf can't hold different types
5. Given two lists val names = listOf("Anna", "Bob", "Cara") and val scores = listOf(90, 85), how can you create a list of strings like ["Anna: 90", "Bob: 85"] using zip with a lambda?
hard
A. names.zip(scores) { name, score -> "$name: $score" }
B. names.zip(scores).map { name, score -> "$name: $score" }
C. names.zipWith(scores) { name, score -> "$name: $score" }
D. names.zip(scores).toString { "$name: $score" }
Solution
Step 1: Use zip with a transform lambda
The zip function can take a lambda to transform pairs directly into desired output.
Step 2: Format each pair as a string
Using { name, score -> "$name: $score" } creates the string format needed.