0
0
Android Kotlinmobile~10 mins

Room database setup 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'
Aid
BuserId
Cuid
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using a field name that is not declared in the data class.
Forgetting to annotate the primary key.
2fill in blank
medium

Complete the code to create a DAO interface with a query to get all users.

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 method names that do not match the query purpose.
Omitting the return type.
3fill in blank
hard

Fix the error in the Room database abstract class declaration.

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

@Database(entities = [User::class], version = 1)
abstract class AppDatabase : [1]() {
    abstract fun userDao(): UserDao
}
Drag options to blanks, or click blank then click option'
ADatabaseBuilder
BDatabase
CRoomBuilder
DRoomDatabase
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect superclass names.
Forgetting to make the class abstract.
4fill in blank
hard

Fill both blanks to build the Room database instance in a singleton pattern.

Android Kotlin
import android.content.Context
import androidx.room.Room

object DatabaseBuilder {
    private var INSTANCE: AppDatabase? = null

    fun getInstance(context: Context): AppDatabase {
        if (INSTANCE == null) {
            INSTANCE = Room.[1](context.applicationContext, AppDatabase::class.java, "app_db").[2]()
        }
        return INSTANCE!!
    }
}
Drag options to blanks, or click blank then click option'
AdatabaseBuilder
Bbuild
Ccreate
Dinitialize
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names for building the database.
Not calling build() to finalize the database instance.
5fill in blank
hard

Fill all three blanks to define an entity with an auto-generated primary key and a non-null column.

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

@Entity
 data class Product(
    @PrimaryKey(autoGenerate = [1]) val id: Int = 0,
    @ColumnInfo(name = "product_name") val [2]: String,
    val price: Double = [3]
)
Drag options to blanks, or click blank then click option'
Atrue
Bfalse
Cname
D0.0
Attempts:
3 left
💡 Hint
Common Mistakes
Setting autoGenerate to false or missing it.
Using wrong field names.
Not providing a default value for price.