0
0
Android Kotlinmobile~20 mins

Room queries (Insert, Update, Delete, Select) in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Room Query Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What is the output of this Room query?
Given this DAO function, what will be the result of calling getAllUsers() if the database has 3 users?
Android Kotlin
import androidx.room.*

@Entity
data class User(
  @PrimaryKey val id: Int,
  val name: String
)

@Dao
interface UserDao {
  @Query("SELECT * FROM User")
  fun getAllUsers(): List<User>
}
AA list of 3 User objects
BA list of 0 User objects
CA single User object
DA Cursor object
Attempts:
2 left
💡 Hint
The query selects all rows from the User table.
📝 Syntax
intermediate
2:00remaining
Which option correctly inserts a User into the database?
Select the correct DAO function to insert a User object into the Room database.
Android Kotlin
import androidx.room.*

@Entity
data class User(
  @PrimaryKey val id: Int,
  val name: String
)

@Dao
interface UserDao {
  // Insert function here
}
A
@Query("INSERT INTO User VALUES(:user)")
fun insertUser(user: User)
B
@Insert
fun insertUser(user: User)
Cfun insertUser(user: User) { database.insert(user) }
D
@Update
fun insertUser(user: User)
Attempts:
2 left
💡 Hint
Use the Room annotation designed for inserting entities.
lifecycle
advanced
2:00remaining
What happens if you update a User with a non-existing ID?
Consider this DAO update function. What will happen if you call it with a User object whose ID does not exist in the database?
Android Kotlin
@Update
fun updateUser(user: User): Int
AA new User is inserted with that ID
BThe function returns 1 and updates a random row
CThe function returns 0 and no rows are updated
DThe function throws an exception
Attempts:
2 left
💡 Hint
Think about how Room handles updates when no matching row exists.
🔧 Debug
advanced
2:00remaining
Why does this delete query cause a runtime error?
This DAO delete function causes a runtime crash. What is the cause?
Android Kotlin
@Query("DELETE FROM User WHERE id = :userId")
fun deleteUser(userId: Int)
AThe function must return Int for number of rows deleted
BThe User entity does not have an id field
CThe DELETE query syntax is invalid
DThe userId parameter type should be Int, not String
Attempts:
2 left
💡 Hint
Check the data type of the primary key in the User entity.
🧠 Conceptual
expert
3:00remaining
How does Room handle conflicts on insert with @Insert(onConflict = OnConflictStrategy.REPLACE)?
If you insert a User with an existing primary key using @Insert(onConflict = OnConflictStrategy.REPLACE), what happens?
AThe existing row is replaced with the new User data
BThe insert is ignored and the old row remains unchanged
CA SQLiteConstraintException is thrown
DBoth rows exist causing duplicate primary keys
Attempts:
2 left
💡 Hint
Consider what REPLACE means in SQLite conflict strategies.