0
0
Kotlinprogramming~20 mins

Sequence creation methods 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 sequence code?
Consider the following Kotlin code that creates and processes a sequence. What will be printed?
Kotlin
val seq = sequenceOf(1, 2, 3, 4, 5).filter { it % 2 == 0 }.map { it * 3 }
println(seq.toList())
A[2, 4, 6, 8, 10]
B[6, 12]
C[3, 6, 9, 12, 15]
D[1, 2, 3, 4, 5]
Attempts:
2 left
💡 Hint
Remember that filter keeps only even numbers, then map multiplies them by 3.
🧠 Conceptual
intermediate
1:30remaining
Which Kotlin sequence creation method produces an infinite sequence?
Among the following Kotlin sequence creation methods, which one can produce an infinite sequence?
AgenerateSequence { (1..10).random() }
BsequenceOf(1, 2, 3)
ClistOf(1, 2, 3).asSequence()
DemptySequence<Int>()
Attempts:
2 left
💡 Hint
Think about which method can keep producing values without a defined end.
🔧 Debug
advanced
2:00remaining
What error does this Kotlin sequence code raise?
Examine this Kotlin code snippet. What error will it cause when run?
Kotlin
val seq = generateSequence(1) { if (it < 5) it + 1 else null }
println(seq.toList())
ANo error, prints [1, 2, 3, 4, 5]
BRuntime error: NullPointerException
CCompilation error: Missing else branch in lambda
DInfinite loop causing OutOfMemoryError
Attempts:
2 left
💡 Hint
Check the lambda's return type and what happens when condition is false.
📝 Syntax
advanced
1:30remaining
Which option correctly creates a Kotlin sequence from a range and filters odd numbers?
Select the Kotlin code snippet that correctly creates a sequence from 1 to 10 and filters only odd numbers.
Aval seq = generateSequence(1..10) { it % 2 == 1 }
Bval seq = sequenceOf(1..10).filter { it % 2 == 1 }
Cval seq = (1..10).asSequence().filter { it % 2 == 1 }
Dval seq = (1..10).filter { it % 2 == 1 }.sequence()
Attempts:
2 left
💡 Hint
Remember how to convert a range to a sequence and apply filter.
🚀 Application
expert
2:30remaining
How many items are in the resulting sequence?
Given this Kotlin code, how many items will the resulting sequence contain?
Kotlin
val seq = generateSequence(1) { if (it < 10) it + 2 else null }
val result = seq.toList()
A9
B6
C10
D5
Attempts:
2 left
💡 Hint
Count the numbers starting at 1, adding 2 each time, stopping before exceeding 10.