0
0
Kotlinprogramming~20 mins

StateFlow for observable state in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
StateFlow 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 StateFlow example?

Consider this Kotlin code using StateFlow. What will be printed?

Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val stateFlow = MutableStateFlow(0)

    launch {
        stateFlow.collect { value ->
            println("Collected: $value")
        }
    }

    delay(100)
    stateFlow.value = 1
    delay(100)
    stateFlow.value = 2
    delay(100)
    cancel()
}
A
Collected: 0
Collected: 1
Collected: 2
B
Collected: 0
Collected: 1
Collected: 1
Collected: 2
C
Collected: 0
Collected: 0
Collected: 1
Collected: 2
D
Collected: 1
Collected: 2
Attempts:
2 left
💡 Hint

Remember that StateFlow always emits the current value immediately to new collectors.

🧠 Conceptual
intermediate
1:30remaining
What happens when multiple collectors subscribe to the same StateFlow?

Given a StateFlow instance, what is true about multiple collectors?

AEach collector receives the current value immediately and all future updates independently.
BOnly the first collector receives updates; others get no values.
CCollectors share a single emission, so only one collector gets each update.
DCollectors receive values only if they subscribe before the first emission.
Attempts:
2 left
💡 Hint

Think about how StateFlow behaves like a hot stream that always holds the latest value.

🔧 Debug
advanced
2:30remaining
Why does this StateFlow collector never print updates?

Look at this Kotlin code snippet. Why does the collector never print any updates?

Kotlin
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*

fun main() = runBlocking {
    val stateFlow = MutableStateFlow(0)

    stateFlow.collect { value ->
        println("Value: $value")
    }

    stateFlow.value = 1
    delay(100)
}
AThe <code>collect</code> call is missing a coroutine scope, so it never starts.
BThe <code>stateFlow.value</code> assignment happens before <code>collect</code>, so no updates are printed.
CThe collector blocks the coroutine, so the value update never happens.
DThe <code>delay</code> is too short to allow collection to start.
Attempts:
2 left
💡 Hint

Consider how collect works in a coroutine and what happens after it.

📝 Syntax
advanced
1:30remaining
Which option correctly creates a StateFlow with initial value 5 and updates it to 10?

Choose the correct Kotlin code snippet that creates a MutableStateFlow with initial value 5 and then updates it to 10.

A
val stateFlow = StateFlow(5)
stateFlow.value = 10
B
val stateFlow = MutableStateFlow(5)
stateFlow.emit(10)
C
val stateFlow = MutableStateFlow&lt;Int&gt;(5)
stateFlow.value = 10
D
val stateFlow = MutableStateFlow(5)
stateFlow.value = 10
Attempts:
2 left
💡 Hint

Remember how to create and update a MutableStateFlow.

🚀 Application
expert
3:00remaining
How to combine two StateFlows to produce a new StateFlow with their sum?

You have two StateFlow<Int> instances: flowA and flowB. Which code snippet correctly creates a new StateFlow<Int> that always holds the sum of the latest values from flowA and flowB?

Aval sumFlow = MutableStateFlow(flowA.value + flowB.value)
Bval sumFlow = flowA.combine(flowB) { a, b -> a + b }.stateIn(scope, SharingStarted.Eagerly, 0)
Cval sumFlow = flowA.map { a -> a + flowB.value } as StateFlow<Int>
Dval sumFlow = flowA.zip(flowB) { a, b -> a + b }.stateIn(scope, SharingStarted.Lazily, 0)
Attempts:
2 left
💡 Hint

Think about how to combine two flows reactively and keep a StateFlow result.