0
0
Android Kotlinmobile~20 mins

Room with Coroutines in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Room Coroutine Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when you call a suspend DAO function from the main thread?
Consider a Room DAO suspend function called directly from the main thread without using any coroutine builder. What will happen?
AThe app crashes with an IllegalStateException.
BThe function returns immediately without executing.
CThe function blocks the main thread until completion.
DThe function runs asynchronously without blocking the main thread.
Attempts:
2 left
💡 Hint
Room enforces thread rules for database access.
🧠 Conceptual
intermediate
2:00remaining
Why use Flow in Room DAO with Coroutines?
What is the main advantage of returning a Flow from a Room DAO query function compared to a suspend function?
ASuspend functions automatically cache data, Flow does not.
BFlow provides continuous updates when the database changes, while suspend functions return data only once.
CFlow runs on the main thread by default, making UI updates easier.
DFlow requires no coroutine context to collect data.
Attempts:
2 left
💡 Hint
Think about live data updates.
lifecycle
advanced
2:00remaining
What happens if you collect a Flow from Room DAO without lifecycle awareness?
If you collect a Flow returned by a Room DAO in a coroutine launched in a ViewModel without tying it to lifecycle, what is a likely consequence?
AThe Flow collection pauses and resumes automatically with lifecycle.
BThe Flow collection automatically stops when the UI is destroyed.
CThe Flow collection throws an exception when UI is destroyed.
DThe Flow collection continues even after the UI is destroyed, causing memory leaks.
Attempts:
2 left
💡 Hint
Consider what happens when UI components are destroyed.
📝 Syntax
advanced
2:00remaining
Which DAO function signature correctly uses suspend with Room?
Choose the correct Kotlin DAO function signature for inserting a User entity using coroutines.
Asuspend fun insertUser(user: User): Long
Bfun insertUser(user: User): Deferred<Long>
Cfun insertUser(user: User): Long
Dsuspend fun insertUser(user: User): Unit
Attempts:
2 left
💡 Hint
Room supports suspend functions returning inserted row id.
🔧 Debug
expert
2:00remaining
Why does this coroutine collecting a Room Flow never emit data?
Given this code snippet in a ViewModel: val usersFlow = userDao.getAllUsers() init { viewModelScope.launch { usersFlow.collect { users -> // update UI } } } The UI never updates even though the database has users. What is the most likely cause?
Android Kotlin
val usersFlow = userDao.getAllUsers()

init {
  viewModelScope.launch {
    usersFlow.collect { users ->
      // update UI
    }
  }
}
AThe usersFlow variable is not marked as suspend.
BThe coroutine is launched on Dispatchers.Main which cannot collect Flow.
CThe getAllUsers() DAO function is not annotated with @Query returning Flow.
DThe collect block is missing a delay causing it to skip emissions.
Attempts:
2 left
💡 Hint
Check DAO function annotations and return types.