0
0
Android Kotlinmobile~10 mins

Entity, DAO, Database classes in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a Room entity with a primary key.

Android Kotlin
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity
 data class User(
    @PrimaryKey val [1]: Int,
    val name: String
)
Drag options to blanks, or click blank then click option'
Akey
BuserId
Cuid
Did
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field name that is not descriptive or conventional for primary keys.
2fill in blank
medium

Complete the DAO interface method to get all users from the database.

Android Kotlin
import androidx.room.Dao
import androidx.room.Query

@Dao
interface UserDao {
    @Query("SELECT * FROM User")
    fun [1](): List<User>
}
Drag options to blanks, or click blank then click option'
AgetAllUsers
BfetchUsers
CloadAll
DretrieveUsers
Attempts:
3 left
💡 Hint
Common Mistakes
Using vague method names that do not clearly indicate the action.
3fill in blank
hard

Fix the error in the database class declaration by completing the annotation.

Android Kotlin
import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [User::class], version = [1])
abstract class AppDatabase : RoomDatabase() {
    abstract fun userDao(): UserDao
}
Drag options to blanks, or click blank then click option'
A-1
B1
C0
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the version to zero or negative values causes runtime errors.
4fill in blank
hard

Fill both blanks to define a DAO method that inserts a user and returns the new row ID.

Android Kotlin
import androidx.room.Dao
import androidx.room.Insert

@Dao
interface UserDao {
    @Insert
    fun [1](user: User): [2]
}
Drag options to blanks, or click blank then click option'
AinsertUser
BLong
CInt
DaddUser
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect return types like Int or void for insert methods.
5fill in blank
hard

Fill all three blanks to define a Room database class with entities, version, and an abstract DAO method.

Android Kotlin
import androidx.room.Database
import androidx.room.RoomDatabase

@Database(entities = [[1]::class], version = [2])
abstract class [3] : RoomDatabase() {
    abstract fun userDao(): UserDao
}
Drag options to blanks, or click blank then click option'
AUser
B1
CAppDatabase
DUserDao
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up class names or using incorrect version numbers.