Room is a library that provides an easy way to work with SQLite databases by using simple annotations and abstracting boilerplate code.
Click to reveal answer
beginner
Which annotation is used to define an entity (table) in Room?
The @Entity annotation is used to mark a Kotlin data class as a table in the Room database.
Click to reveal answer
beginner
What is the role of the @Dao interface in Room?
The @Dao interface defines methods to access the database, such as queries, inserts, updates, and deletes.
Click to reveal answer
intermediate
How do you create the Room database instance in your app?
You create the database instance using Room.databaseBuilder(context, YourDatabase::class.java, "database_name").build() inside a singleton or application class.
Click to reveal answer
intermediate
Why should Room database operations be done off the main thread?
Because database operations can be slow, doing them on the main thread can freeze the UI. Room enforces this by throwing an error if you try to do heavy operations on the main thread.
Click to reveal answer
Which annotation marks a data class as a table in Room?
A@Database
B@Dao
C@Table
D@Entity
✗ Incorrect
The @Entity annotation is used to define a table in Room.
What does @Dao stand for in Room?
AData Access Object
BDatabase Access Operation
CData Annotation Object
DDirect Access Operation
✗ Incorrect
@Dao stands for Data Access Object, which defines database interaction methods.
Room.databaseBuilder() is the correct way to create a database instance.
Which of these is NOT a recommended practice with Room?
ARunning queries on the main thread
BUsing @Entity for tables
CDefining DAO interfaces
DUsing LiveData with queries
✗ Incorrect
Running queries on the main thread can freeze the UI and is not recommended.
What is the purpose of the @Database annotation?
ATo create tables automatically
BTo mark a class as a DAO
CTo define the database holder and list entities
DTo run queries
✗ Incorrect
@Database annotation defines the database class and its entities.
Explain the main components needed to set up a Room database in an Android app.
Think about the classes and annotations you need to define tables, access data, and create the database.
You got /4 concepts.
Why is it important to avoid running Room database operations on the main thread? How can you handle this?
Consider what happens if the app waits too long for data and how Kotlin helps with background work.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @Entity annotation in Room database setup?
easy
A. To define a table in the database
B. To create a database instance
C. To write SQL queries
D. To start a database transaction
Solution
Step 1: Understand the role of @Entity
The @Entity annotation marks a Kotlin data class as a table in the Room database.
Step 2: Differentiate from other annotations
Other annotations like @Dao define data access objects, not tables.
Final Answer:
To define a table in the database -> Option A
Quick Check:
@Entity = Table definition [OK]
Hint: Remember: @Entity means table in Room [OK]
Common Mistakes:
Confusing @Entity with @Dao
Thinking @Entity creates the database
Mixing @Entity with SQL query annotations
2. Which of the following is the correct way to declare a Room database class in Kotlin?
easy
A. @Dao(entities = [User::class], version = 1)\ninterface AppDatabase {\n fun userDao(): UserDao\n}
B. @Database(entities = [User::class], version = 1)\nabstract class AppDatabase : RoomDatabase() {\n abstract fun userDao(): UserDao\n}
C. @Entity(entities = [User::class], version = 1)\nclass AppDatabase : RoomDatabase() {\n fun userDao(): UserDao\n}
D. @Database(entities = User, version = 1)\nclass AppDatabase() {\n fun userDao(): UserDao\n}
Solution
Step 1: Identify correct annotation and class type
The Room database class must be annotated with @Database and extend RoomDatabase.
Step 2: Check syntax for entities and abstract DAO method
The entities list must be an array of classes, and DAO methods must be abstract.
Final Answer:
@Database(entities = [User::class], version = 1)\nabstract class AppDatabase : RoomDatabase() {\n abstract fun userDao(): UserDao\n} -> Option B
Quick Check:
@Database + abstract class + extends RoomDatabase = correct [OK]
Hint: Database class is abstract and annotated @Database [OK]
Common Mistakes:
Using @Entity instead of @Database
Not making the class abstract
Forgetting to extend RoomDatabase
3. Given the following code snippet, what will be the result of calling db.userDao().getAllUsers()?
@Dao
interface UserDao {
@Query("SELECT * FROM user")
fun getAllUsers(): List<User>
}
@Entity
data class User(
@PrimaryKey val id: Int,
val name: String
)
// Assume database is properly created and contains 3 users
medium
A. A runtime error because of missing suspend keyword
B. An empty list
C. A list of 3 User objects from the database
D. A compilation error due to missing @Insert method
Solution
Step 1: Understand the DAO query
The @Query("SELECT * FROM user") fetches all rows from the user table.
Step 2: Check the database state
The question states the database contains 3 users, so the query returns 3 User objects.
Final Answer:
A list of 3 User objects from the database -> Option C
Quick Check:
Query returns all users = 3 users [OK]
Hint: Query returns all rows; count matches stored data [OK]
Common Mistakes:
Assuming empty list without data check
Confusing suspend functions with normal DAO methods
Expecting errors without code context
4. Identify the error in this Room database builder code snippet:
val db = Room.databaseBuilder(
context,
AppDatabase::class.java,
"app_db"
).build()
medium
A. Missing fallbackToDestructiveMigration() causing crash on schema change
B. Incorrect database name format, should be a URI
C. AppDatabase must be an interface, not a class
D. databaseBuilder requires a coroutine scope parameter
Solution
Step 1: Review databaseBuilder usage
The code correctly builds the database but lacks migration strategy.
Step 2: Understand migration impact
Without fallbackToDestructiveMigration(), schema changes cause runtime crashes.
Final Answer:
Missing fallbackToDestructiveMigration() causing crash on schema change -> Option A
Quick Check:
Missing migration fallback = crash risk [OK]
Hint: Add fallbackToDestructiveMigration() to avoid crashes [OK]
Common Mistakes:
Thinking database name must be URI
Confusing class vs interface for database
Expecting coroutine scope in builder
5. You want to add a new column age to your existing User entity without losing existing data. Which approach correctly updates the Room database?
hard
A. Change the database name to force a new database creation
B. Just add the new field in the User class and keep the same database version
C. Delete the app data and reinstall to reset the database
D. Increase database version and provide a migration object with SQL to add the new column
Solution
Step 1: Understand schema changes in Room
Adding a new column changes the database schema, so version must increase.
Step 2: Provide migration to preserve data
Migration object with SQL ALTER TABLE adds the column without losing data.
Final Answer:
Increase database version and provide a migration object with SQL to add the new column -> Option D
Quick Check:
Schema change needs version bump + migration [OK]
Hint: Always migrate with version bump to keep data safe [OK]
Common Mistakes:
Changing entity without version update
Deleting app data instead of migrating
Changing database name to avoid migration
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.