0
0
Android Kotlinmobile~20 mins

Database migrations in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Database Migration Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens after a database migration?
You have an Android app using Room database. You add a new column to a table and write a migration from version 1 to 2. What will the app do after updating if the migration is correct?
AThe app crashes because the schema version is higher than before.
BThe app deletes all existing data and creates a new database.
CThe app updates the database schema without losing existing data.
DThe app ignores the migration and uses the old schema.
Attempts:
2 left
💡 Hint
Think about what a migration is supposed to do to keep data safe.
📝 Syntax
intermediate
2:00remaining
Identify the correct Room migration code snippet
Which Kotlin code snippet correctly defines a migration from version 1 to 2 that adds a new column 'age' to the 'User' table with a default value 0?
Android Kotlin
val MIGRATION_1_2 = object : Migration(1, 2) {
  override fun migrate(database: SupportSQLiteDatabase) {
    // migration code here
  }
}
Adatabase.execSQL("DROP TABLE User")
Bdatabase.execSQL("ALTER TABLE User ADD COLUMN age INTEGER NOT NULL DEFAULT 0")
Cdatabase.execSQL("CREATE TABLE User(age INTEGER)")
Ddatabase.execSQL("ALTER TABLE User ADD COLUMN age TEXT")
Attempts:
2 left
💡 Hint
Adding a non-null column requires a default value in SQLite.
lifecycle
advanced
2:00remaining
When is the migration code executed in an Android app?
At what point during the app lifecycle does Room execute the migration code when upgrading the database version?
AWhen the app is uninstalled and reinstalled.
BImmediately when the app starts, before any UI loads.
COnly when the user triggers a manual database refresh.
DWhen the database is first opened or accessed after the app update.
Attempts:
2 left
💡 Hint
Think about when the app needs to access the database after an update.
🔧 Debug
advanced
2:00remaining
Why does this migration cause a crash?
You wrote this migration code: val MIGRATION_2_3 = object : Migration(2, 3) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE User ADD COLUMN email TEXT NOT NULL") } } The app crashes on upgrade. Why?
AAdding a NOT NULL column without a default value causes a constraint error.
BThe SQL syntax for ALTER TABLE is incorrect.
CThe migration version numbers are reversed.
DThe User table does not exist in version 2.
Attempts:
2 left
💡 Hint
SQLite requires a default value when adding NOT NULL columns.
🧠 Conceptual
expert
3:00remaining
What is the best practice for handling complex schema changes in Room migrations?
You need to rename a column and change its type in a Room database migration. SQLite does not support renaming columns directly. What is the recommended approach?
ACreate a new table with the desired schema, copy data from the old table, drop the old table, then rename the new table.
BDelete the database and recreate it with the new schema.
CUse ALTER TABLE to rename the column and change its type directly.
DIgnore the change and keep the old schema.
Attempts:
2 left
💡 Hint
Think about how to preserve data when SQLite lacks direct support for renaming columns.