0
0
Android Kotlinmobile~20 mins

SharedFlow for events in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
SharedFlow Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when multiple collectors subscribe to a SharedFlow?
Consider a SharedFlow that emits integer events. If two collectors start collecting from this SharedFlow, what will they receive?
Android Kotlin
val sharedFlow = MutableSharedFlow<Int>()

// Collector 1
sharedFlow.onEach { println("Collector 1 received: $it") }.launchIn(scope)

// Collector 2
sharedFlow.onEach { println("Collector 2 received: $it") }.launchIn(scope)

sharedFlow.emit(5)
AOnly the first collector receives the event 5.
BBoth collectors receive the event 5.
COnly the second collector receives the event 5.
DNeither collector receives the event 5.
Attempts:
2 left
💡 Hint
SharedFlow is designed to broadcast events to all active collectors.
lifecycle
intermediate
2:00remaining
How does SharedFlow behave when collectors start after emission?
If a MutableSharedFlow is created with default settings and an event is emitted before any collector starts, what happens when a collector starts collecting later?
Android Kotlin
val sharedFlow = MutableSharedFlow<Int>()

sharedFlow.emit(10)

// Collector starts after emit
sharedFlow.onEach { println("Received: $it") }.launchIn(scope)
AThe collector receives a default value 0.
BThe code throws an exception.
CThe collector does not receive the event 10.
DThe collector receives the event 10.
Attempts:
2 left
💡 Hint
Default SharedFlow does not replay events to late collectors.
📝 Syntax
advanced
2:00remaining
What is the correct way to create a SharedFlow that replays the last event?
You want a SharedFlow that replays the last emitted event to new collectors. Which code snippet correctly creates such a SharedFlow?
Aval sharedFlow = MutableSharedFlow<Int>(replay = 1)
Bval sharedFlow = MutableSharedFlow<Int>(bufferSize = 1)
Cval sharedFlow = MutableSharedFlow<Int>(cache = 1)
Dval sharedFlow = MutableSharedFlow<Int>(replayCount = 1)
Attempts:
2 left
💡 Hint
The parameter to control replay is named 'replay'.
🔧 Debug
advanced
2:00remaining
Why does this SharedFlow emit block suspend indefinitely?
Given this code, why does the call to emit suspend forever?
Android Kotlin
val sharedFlow = MutableSharedFlow<Int>(replay = 0, extraBufferCapacity = 0)

// No collectors started
sharedFlow.emit(1) // Suspends here
ABecause emit requires at least one collector to be started before calling.
BBecause replay is zero, emit cannot send events.
CBecause extraBufferCapacity is zero, emit throws an exception.
DBecause there is no buffer and no active collectors, emit suspends waiting for a collector.
Attempts:
2 left
💡 Hint
Emit suspends if there is no buffer and no collector to receive the event.
🧠 Conceptual
expert
3:00remaining
How to ensure events are not lost when no collectors are active?
You want to use a SharedFlow to send UI events that must not be lost even if no collector is active at the moment of emission. Which configuration ensures this behavior?
ACreate MutableSharedFlow with replay = 1 and extraBufferCapacity = 1.
BCreate MutableSharedFlow with replay = 0 and extraBufferCapacity = 1.
CCreate MutableSharedFlow with replay = 1 and extraBufferCapacity = 0.
DCreate MutableSharedFlow with replay = 0 and extraBufferCapacity = 0.
Attempts:
2 left
💡 Hint
Replay buffers past events; extraBufferCapacity buffers new events when no collector is ready.