0
0
Android Kotlinmobile~10 mins

Repository pattern 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 declare a repository interface in Kotlin.

Android Kotlin
interface UserRepository {
    fun getUserById(id: Int): [1]
}
Drag options to blanks, or click blank then click option'
AInt
BString
CBoolean
DUser
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a primitive type like Int or Boolean instead of a User object.
Using String as return type which does not represent a user.
2fill in blank
medium

Complete the code to implement the repository interface with a local data source.

Android Kotlin
class LocalUserRepository : UserRepository {
    override fun getUserById(id: Int): [1] {
        // fetch user from local database
        return User(id, "Name")
    }
}
Drag options to blanks, or click blank then click option'
AString
BInt
CUser
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a String or other type instead of User.
Not overriding the method correctly.
3fill in blank
hard

Fix the error in the repository method signature to make it suspendable for coroutine support.

Android Kotlin
interface UserRepository {
    suspend fun getUserById(id: Int): [1]
}
Drag options to blanks, or click blank then click option'
AString
BUser
CUnit
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the return type to Unit or Boolean which does not represent user data.
Removing suspend keyword which is required for coroutine support.
4fill in blank
hard

Fill both blanks to create a repository method that fetches users from a remote source and returns a list.

Android Kotlin
interface UserRepository {
    suspend fun getAllUsers(): [1]<[2]>
}
Drag options to blanks, or click blank then click option'
AList
BUser
CSet
DString
Attempts:
3 left
💡 Hint
Common Mistakes
Using Set instead of List which is less common for ordered user lists.
Returning String which does not represent user data.
5fill in blank
hard

Fill all three blanks to implement a repository method that saves a user and returns a success flag.

Android Kotlin
interface UserRepository {
    suspend fun saveUser(user: [1]): [2]
}

class UserRepositoryImpl : UserRepository {
    override suspend fun saveUser(user: [3]): Boolean {
        // save user logic
        return true
    }
}
Drag options to blanks, or click blank then click option'
AUser
BBoolean
CString
DInt
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching parameter types between interface and implementation.
Returning String or Int instead of Boolean.