2. Which of the following is the correct way to declare a suspend function in a Room DAO interface?
easy
A. suspend fun getAllUsers(): List<User>
B. fun getAllUsers(): List<User>
C. async fun getAllUsers(): List<User>
D. fun suspend getAllUsers(): List<User>
Solution
Step 1: Recall suspend function syntax
A suspend function is declared with the keyword suspend before fun.
Step 2: Check each option
suspend fun getAllUsers(): List<User> correctly uses suspend fun. Plain fun getAllUsers(): List<User> lacks the suspend keyword. fun suspend getAllUsers(): List<User> has the keywords in wrong order. async fun getAllUsers(): List<User> uses async which is not valid here.
Final Answer:
suspend fun getAllUsers(): List<User> -> Option A
Quick Check:
suspend keyword before fun = B [OK]
Hint: Remember: suspend keyword always comes before fun [OK]
Common Mistakes:
Placing suspend after fun
Using async instead of suspend
Omitting suspend keyword for coroutine functions
3. Given this DAO function:
@Query("SELECT * FROM users")
suspend fun getUsers(): List<User>
What happens when you call getUsers() from a coroutine?
medium
A. The function blocks the main thread until data is fetched.
B. The function suspends the coroutine until data is fetched, then returns the list.
C. The function returns immediately with an empty list.
D. The function throws a compile-time error.
Solution
Step 1: Understand suspend function behavior
A suspend function pauses the coroutine without blocking the thread until the result is ready.
Step 2: Apply to Room DAO call
Calling getUsers() suspends the coroutine until Room fetches data, then returns the list.
Final Answer:
The function suspends the coroutine until data is fetched, then returns the list. -> Option B
Quick Check:
suspend functions pause coroutine, not block thread = C [OK]
Hint: Suspend functions pause coroutine, not block main thread [OK]
Common Mistakes:
Thinking suspend blocks main thread
Expecting immediate empty list return
Assuming compile-time error on suspend
4. You wrote this DAO method:
@Query("SELECT * FROM users")
fun getUsers(): List<User>
When calling getUsers() from a coroutine, what problem will occur?
medium
A. The app will crash with a runtime error about main thread database access.
B. The function will suspend correctly and return data.
C. The function will return null instead of data.
D. The function will compile but never return.
Solution
Step 1: Identify missing suspend keyword
The DAO method is not marked suspend, so it runs on the calling thread.
Step 2: Understand Room's main thread policy
Room forbids database queries on the main thread to avoid freezing UI, causing runtime crash if violated.
Final Answer:
The app will crash with a runtime error about main thread database access. -> Option A
Quick Check:
Non-suspend DAO on main thread causes crash = A [OK]
Hint: Always mark DAO queries suspend to avoid main thread crash [OK]
Common Mistakes:
Forgetting suspend keyword in DAO
Expecting null return on wrong thread
Assuming silent failure without crash
5. You want to insert a list of users into the database using Room and Coroutines. Which DAO method signature correctly supports this operation without blocking the UI?
@Insert
??? fun insertUsers(users: List<User>)
hard
A. fun insertUsers(users: List<User>)
B. async fun insertUsers(users: List<User>)
C. fun insertUsers(users: List<User>): Deferred<Unit>
D. suspend fun insertUsers(users: List<User>)
Solution
Step 1: Identify correct coroutine support
To run database operations without blocking UI, DAO methods must be suspend functions.
Step 2: Evaluate options
suspend fun insertUsers(users: List<User>) uses suspend fun, which is correct. fun insertUsers(users: List<User>) blocks UI. fun insertUsers(users: List<User>): Deferred<Unit> returns Deferred which is not standard DAO signature. async fun insertUsers(users: List<User>) uses invalid async keyword.
Final Answer:
suspend fun insertUsers(users: List<User>) -> Option D
Quick Check:
Use suspend fun for non-blocking Room inserts = D [OK]
Hint: Use suspend fun for DAO inserts to keep UI smooth [OK]
Common Mistakes:
Omitting suspend causing UI freeze
Using async keyword incorrectly
Returning Deferred instead of suspend function
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.