0
0
Android Kotlinmobile~20 mins

Room database setup in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Room Database Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
2:00remaining
Correct Room Entity Declaration
Which option correctly declares a Room entity with a primary key named id of type Int?
A
@Entity
 data class User(
  @PrimaryKey val id: Int,
  val name: String
 )
B
@Entity
 data class User(
  val id: Int,
  @PrimaryKey val name: String
 )
C
data class User(
  @PrimaryKey val id: Int,
  val name: String
 )
D
@Entity
 data class User(
  @PrimaryKey(autoGenerate = true) val name: String,
  val id: Int
 )
Attempts:
2 left
💡 Hint
Remember the @Entity annotation is required on the data class and @PrimaryKey must be on the primary key property.
ui_behavior
intermediate
2:00remaining
Room Database Builder Behavior
What happens if you call Room.databaseBuilder(context, AppDatabase::class.java, "db_name").build() multiple times in your app?
AEach call creates a new database instance, which can cause unexpected behavior and resource waste.
BRoom automatically returns the same singleton instance for all calls.
CThe database file is deleted and recreated each time.
DThe app crashes with an IllegalStateException.
Attempts:
2 left
💡 Hint
Think about how Room manages database instances and best practices for usage.
lifecycle
advanced
2: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?
ARoom automatically updates the database without any issues.
BThe app crashes with an IllegalStateException about missing migration.
CThe app ignores the new column and continues using the old schema.
DThe database is deleted and recreated silently.
Attempts:
2 left
💡 Hint
Room requires explicit migration strategies to handle schema changes safely.
navigation
advanced
2:00remaining
Accessing DAO from ViewModel
What is the best way to access a Room DAO inside a ViewModel in an Android app?
ACreate a new database instance inside the ViewModel and get the DAO.
BUse a global variable to hold the DAO and access it inside the ViewModel.
CPass the DAO instance to the ViewModel constructor using a ViewModelFactory.
DAccess the DAO directly from the Activity and pass data to ViewModel methods.
Attempts:
2 left
💡 Hint
Think about separation of concerns and lifecycle safety.
🧠 Conceptual
expert
2:00remaining
Room Database Threading Behavior
Which statement best describes Room's default behavior regarding database operations and threading?
ARoom queues database operations and runs them sequentially on the main thread.
BRoom allows database operations on the main thread by default.
CRoom automatically runs all queries on a background thread.
DRoom throws an exception if you perform database operations on the main thread.
Attempts:
2 left
💡 Hint
Consider Android's UI thread and responsiveness rules.