Challenge - 5 Problems
Room Database Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate2:00remaining
Correct Room Entity Declaration
Which option correctly declares a Room entity with a primary key named
id of type Int?Attempts:
2 left
💡 Hint
Remember the
@Entity annotation is required on the data class and @PrimaryKey must be on the primary key property.✗ Incorrect
Option A correctly uses
@Entity on the data class and marks id as the primary key. Option A wrongly marks name as primary key. Option A misses the @Entity annotation. Option A wrongly uses name as primary key and swaps property order.❓ ui_behavior
intermediate2:00remaining
Room Database Builder Behavior
What happens if you call
Room.databaseBuilder(context, AppDatabase::class.java, "db_name").build() multiple times in your app?Attempts:
2 left
💡 Hint
Think about how Room manages database instances and best practices for usage.
✗ Incorrect
Calling
databaseBuilder().build() multiple times creates separate instances. This can cause conflicts and waste resources. The recommended approach is to create a singleton instance and reuse it.❓ lifecycle
advanced2:00remaining
Room Database Migration Handling
If you change your Room database schema by adding a new column without providing a migration strategy, what will happen when the app runs?
Attempts:
2 left
💡 Hint
Room requires explicit migration strategies to handle schema changes safely.
✗ Incorrect
Room enforces schema consistency. Without a migration, it throws an IllegalStateException to prevent data loss. You must provide a migration or fallback strategy.
advanced
2:00remaining
Accessing DAO from ViewModel
What is the best way to access a Room DAO inside a ViewModel in an Android app?
Attempts:
2 left
💡 Hint
Think about separation of concerns and lifecycle safety.
✗ Incorrect
Passing the DAO via ViewModelFactory keeps the ViewModel testable and lifecycle-aware. Creating new database instances or using globals can cause resource issues or tight coupling.
🧠 Conceptual
expert2:00remaining
Room Database Threading Behavior
Which statement best describes Room's default behavior regarding database operations and threading?
Attempts:
2 left
💡 Hint
Consider Android's UI thread and responsiveness rules.
✗ Incorrect
Room enforces that database operations do not run on the main thread to avoid UI freezes. It throws a runtime exception if you try to do so unless you explicitly allow main thread queries (not recommended).