What does the map operator do in a Kotlin sequence?
The map operator transforms each element in the sequence by applying a given function, creating a new sequence with the transformed elements.
Click to reveal answer
beginner
What is the purpose of the filter operator in Kotlin sequences?
The filter operator selects elements from the sequence that satisfy a given condition, creating a new sequence with only those elements.
Click to reveal answer
intermediate
How does Kotlin's Sequence differ from a regular List when using map and filter?
A Sequence processes elements lazily, meaning operations like map and filter are applied only when needed, improving performance for large data sets compared to eager List operations.
Click to reveal answer
beginner
What will be the output of this Kotlin code?
val numbers = sequenceOf(1, 2, 3, 4, 5)
val result = numbers.filter { it % 2 == 0 }.map { it * 10 }.toList()
println(result)
The output will be [20, 40]. First, filter selects even numbers (2 and 4), then map multiplies each by 10.
Click to reveal answer
intermediate
Why is chaining map and filter on sequences efficient in Kotlin?
Because sequences use lazy evaluation, chaining map and filter does not create intermediate collections. Elements are processed one by one through the chain, saving memory and time.
Click to reveal answer
What does the filter operator do in a Kotlin sequence?
ASelects elements that meet a condition
BTransforms each element using a function
CSorts the elements
DRemoves duplicates
✗ Incorrect
filter keeps only elements that satisfy the given condition.
Which of the following is true about Kotlin sequences?
AThey process elements eagerly
BThey process elements lazily
CThey cannot be chained with <code>map</code> or <code>filter</code>
DThey always return a list
✗ Incorrect
Kotlin sequences use lazy evaluation, processing elements only when needed.
What will sequenceOf(1, 2, 3).map { it * 2 }.toList() return?
A[1, 2, 3]
BAn error
C[2, 4, 6]
D[1, 4, 9]
✗ Incorrect
The map multiplies each element by 2, resulting in [2, 4, 6].
Why might you prefer sequences over lists for large data sets?
ASequences process elements lazily, improving performance
BSequences use more memory
CSequences are easier to write
DSequences do not support <code>map</code> or <code>filter</code>
✗ Incorrect
Lazy processing avoids creating intermediate collections, saving memory and time.
What does this code print?
val seq = sequenceOf(1, 2, 3, 4)
val res = seq.filter { it > 2 }.map { it + 1 }.toList()
println(res)
A[3, 4, 5]
B[1, 2]
C[2, 3, 4]
D[4, 5]
✗ Incorrect
Filter keeps 3 and 4, map adds 1 to each, resulting in [4, 5].
Explain how map and filter work together in Kotlin sequences.
Think about how each operator changes the sequence and how they combine.
You got /4 concepts.
Describe the benefits of using sequences with map and filter compared to lists.
Consider how sequences handle data differently than lists.
You got /4 concepts.
Practice
(1/5)
1. What does the map operator do in a Kotlin sequence?
easy
A. It removes items that do not match a condition.
B. It transforms each item in the sequence to a new value.
C. It sorts the items in the sequence.
D. It counts the number of items in the sequence.
Solution
Step 1: Understand the purpose of map
The map operator applies a function to each item, changing it to a new value.
Step 2: Compare with other operators
filter removes items, sort orders items, and count counts items, so these are different.
Final Answer:
It transforms each item in the sequence to a new value. -> Option B
Hint: filter uses braces with condition inside [OK]
Common Mistakes:
Using map instead of filter
Missing curly braces in lambda
Wrong condition inside lambda
3. What is the output of this Kotlin code?
val numbers = sequenceOf(1, 2, 3, 4)
val result = numbers.filter { it % 2 == 0 }.map { it * 10 }.toList()
println(result)
medium
A. [10, 20]
B. [2, 4]
C. [20, 40]
D. [1, 3]
Solution
Step 1: Apply filter to keep even numbers
Filter keeps numbers where it % 2 == 0, so 2 and 4 remain.
Step 2: Apply map to multiply each by 10
Mapping multiplies each by 10: 2 * 10 = 20, 4 * 10 = 40.
Final Answer:
[20, 40] -> Option C
Quick Check:
filter evens then map *10 = [20, 40] [OK]
Hint: Filter first, then map transforms filtered items [OK]
Common Mistakes:
Mapping before filtering
Confusing filter condition
Forgetting to convert sequence to list
4. Find the error in this Kotlin code snippet:
val seq = sequenceOf(1, 2, 3, 4)
val filtered = seq.filter(it % 2 == 0)
println(filtered.toList())
medium
A. Missing curly braces around lambda in filter
B. Using map instead of filter
C. Sequence cannot be converted to list
D. Filter condition should be it % 2 != 0
Solution
Step 1: Check filter lambda syntax
Filter requires a lambda with curly braces: { it % 2 == 0 }. The code misses braces.
Step 2: Validate other options
Map is not used here; sequence can convert to list; condition can be any boolean expression.
Final Answer:
Missing curly braces around lambda in filter -> Option A
Quick Check:
filter needs braces for lambda [OK]
Hint: Always use braces {} for lambdas in filter [OK]
Common Mistakes:
Omitting braces in lambda
Confusing filter and map
Wrong filter condition assumed as error
5. Given a sequence of strings seq = sequenceOf("apple", "banana", "cherry", "date"), which Kotlin code correctly filters strings containing letter 'a' and converts them to uppercase?
hard
A. seq.filter { it.contains('a') }.map { it.lowercase() }.toList()
B. seq.map { it.uppercase() }.filter { it.contains('a') }.toList()
C. seq.map { it.lowercase() }.filter { it.contains('a') }.toList()
D. seq.filter { it.contains('a') }.map { it.uppercase() }.toList()
Solution
Step 1: Filter strings containing 'a'
Use filter { it.contains('a') } to keep only strings with 'a'. This keeps "apple", "banana", "date".
Step 2: Convert filtered strings to uppercase
Use map { it.uppercase() } to change these strings to uppercase.
Final Answer:
seq.filter { it.contains('a') }.map { it.uppercase() }.toList() -> Option D