0
0
Android Kotlinmobile~20 mins

Entity, DAO, Database classes in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Room Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:00remaining
What is the role of an Entity class in Android Room?
In Android Room, what does an Entity class represent?
AA table in the database with columns as class fields
BA class that manages database connections
CA class that handles database queries
DA UI component for displaying data
Attempts:
2 left
💡 Hint
Think about how data is stored in a database.
ui_behavior
intermediate
1:30remaining
What happens when you call a DAO query method on the main thread?
Consider a DAO method annotated with @Query that fetches data. What happens if you call this method directly on the main thread?
AThe query runs asynchronously automatically
BThe query runs smoothly without any issues
CThe app shows a warning but continues
DThe app crashes with a runtime exception
Attempts:
2 left
💡 Hint
Think about Android's rule for long operations on the main thread.
lifecycle
advanced
1:30remaining
When is the Room database instance created in a typical Android app?
In an Android app using Room, when is the database instance usually created?
AOnly when the user opens a specific screen
BEvery time a DAO method is called
CWhen the app starts, typically in a singleton or Application class
DWhen the app goes to background
Attempts:
2 left
💡 Hint
Think about how to avoid creating multiple database instances.
📝 Syntax
advanced
2:00remaining
What is the correct syntax to define a DAO method that inserts a User entity?
Choose the correct Kotlin DAO method syntax to insert a User entity into the database.
Android Kotlin
interface UserDao {
    // Insert method here
}
A@Insert fun insertUser(user: User): Int
B@Insert suspend fun insertUser(user: User)
Cfun insertUser(@Insert user: User)
D@Insert suspend fun insertUser(): User
Attempts:
2 left
💡 Hint
The @Insert annotation goes before the function, and suspend is used for coroutines.
🔧 Debug
expert
2:30remaining
Why does this Room database build fail with "Cannot find symbol" error?
Given this code snippet, why does the build fail with a "Cannot find symbol" error for the DAO interface? @Entity data class Product(val id: Int, val name: String) @Database(entities = [Product::class], version = 1) abstract class AppDatabase : RoomDatabase() { abstract fun productDao(): ProductDao } // ProductDao interface is missing in the project.
ABecause the ProductDao interface is not defined anywhere
BBecause the Product entity is missing the @PrimaryKey annotation
CBecause the database version is not incremented
DBecause RoomDatabase class is not imported
Attempts:
2 left
💡 Hint
Check if all referenced classes exist in the project.