0
0
Kotlinprogramming~20 mins

Why Flow matters for async sequences in Kotlin - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Flow 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 Flow code?
Consider this Kotlin Flow code that emits numbers with delay. What will be printed?
Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val flow = flow {
        for (i in 1..3) {
            delay(100)
            emit(i)
        }
    }
    flow.collect { value ->
        println("Received $value")
    }
}
AReceived 1\nReceived 2\nReceived 3
BReceived 3\nReceived 2\nReceived 1
CReceived 1\nReceived 1\nReceived 1
DNo output, program hangs
Attempts:
2 left
💡 Hint
Flow emits values sequentially with delay, collect prints each as it arrives.
🧠 Conceptual
intermediate
1:30remaining
Why use Flow instead of a simple suspend function returning a list?
Which reason best explains why Kotlin Flow is preferred for asynchronous sequences over a suspend function that returns a full list?
AFlow always runs on the main thread, making UI updates easier.
BSuspend functions cannot return lists in Kotlin.
CFlow emits values one by one asynchronously, allowing processing before all data is ready.
DSuspend functions are slower than Flow in all cases.
Attempts:
2 left
💡 Hint
Think about how data arrives and is processed over time.
🔧 Debug
advanced
2:00remaining
What error does this Flow code produce?
This Kotlin code tries to collect from a Flow but crashes. What error is thrown?
Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val flow = flow {
        emit(1)
        throw RuntimeException("Oops")
    }
    flow.collect { value ->
        println(value)
    }
}
ANo error, prints 1 then stops
BNullPointerException
CCompilation error: missing catch block
DRuntimeException with message 'Oops'
Attempts:
2 left
💡 Hint
Look at what happens after emitting the first value.
📝 Syntax
advanced
1:30remaining
Which option correctly creates a Flow emitting squares of 1 to 3?
Choose the Kotlin code snippet that correctly creates a Flow emitting 1, 4, 9.
Aflow { (1..3).map { emit(it * it) } }
Bflow { for (i in 1..3) emit(i * i) }
Cflow { for i in 1..3 emit(i * i) }
Dflow { emit(i * i) for i in 1..3 }
Attempts:
2 left
💡 Hint
Remember Kotlin syntax for loops and emit inside flow builder.
🚀 Application
expert
2:30remaining
How many items does this Flow emit before cancellation?
Given this Kotlin Flow code with cancellation, how many values are printed before the program stops?
Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val flow = flow {
        var i = 1
        while (true) {
            emit(i++)
            delay(50)
        }
    }
    val job = launch {
        flow.collect { value ->
            println(value)
            if (value == 3) cancel()
        }
    }
    job.join()
}
A3
B4
CInfinite, program never stops
D2
Attempts:
2 left
💡 Hint
Look when cancellation happens inside collect.