0
0
Kotlinprogramming~20 mins

Sequence operators (map, filter) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sequence Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using map and filter?
Consider this Kotlin code that uses sequence operators map and filter. What will it print?
Kotlin
val numbers = sequenceOf(1, 2, 3, 4, 5)
val result = numbers.filter { it % 2 == 1 }.map { it * 3 }.toList()
println(result)
A[6, 12, 18]
B[2, 4]
C[3, 9, 15]
D[1, 3, 5]
Attempts:
2 left
💡 Hint
First filter odd numbers, then multiply each by 3.
Predict Output
intermediate
2:00remaining
What does this Kotlin code print after filtering and mapping?
Look at this Kotlin code using sequence operators. What is the output?
Kotlin
val words = sequenceOf("apple", "banana", "cherry", "date")
val result = words.filter { it.length > 5 }.map { it.uppercase() }.toList()
println(result)
A["BANANA", "CHERRY"]
B["apple", "banana", "cherry", "date"]
C["APPLE", "BANANA", "CHERRY", "DATE"]
D["date"]
Attempts:
2 left
💡 Hint
Filter words longer than 5 letters, then convert to uppercase.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin code throw an exception?
This Kotlin code throws an exception. What is the cause?
Kotlin
val nums = sequenceOf(1, 2, 3, 4)
val result = nums.map { if (it == 3) null else it * 2 }.filter { it!! > 2 }.toList()
println(result)
AThrows ClassCastException due to null in sequence
BThrows IllegalArgumentException because map returns null
CNo exception, prints [4, 8]
DThrows NullPointerException because filter uses 'it!!' on null values
Attempts:
2 left
💡 Hint
Check how null values are handled in filter with 'it!!'.
📝 Syntax
advanced
2:00remaining
Which option is the correct Kotlin syntax to filter even numbers and double them using sequences?
Choose the correct Kotlin code that filters even numbers from a sequence and doubles them.
Aval result = sequenceOf(1,2,3,4).filter { it % 2 == 0 }.map { it * 2 }.toList()
Bval result = sequenceOf(1,2,3,4).map { it * 2 }.filter { it % 2 == 0 }.toList()
Cval result = sequenceOf(1,2,3,4).filter(it % 2 == 0).map(it * 2).toList()
Dval result = sequenceOf(1,2,3,4).filter { it % 2 == 0 }.map(it * 2).toList()
Attempts:
2 left
💡 Hint
Remember to use lambda syntax with curly braces and 'it'.
🚀 Application
expert
2:00remaining
How many items are in the resulting list after this Kotlin sequence operation?
Given this Kotlin code, how many items does the resulting list contain?
Kotlin
val data = sequenceOf(10, 15, 20, 25, 30)
val result = data.filter { it > 15 }.map { it / 5 }.filter { it % 2 == 0 }.toList()
println(result.size)
A1
B2
C3
D4
Attempts:
2 left
💡 Hint
Step through filtering and mapping carefully.