0
0
Android Kotlinmobile~10 mins

Repository pattern in depth 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
CUser
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a primitive type like Int or String instead of a User object.
Forgetting to specify the return type.
2fill in blank
medium

Complete the code to implement the repository class that fetches data from a local data source.

Android Kotlin
class UserRepositoryImpl(private val localDataSource: LocalDataSource) : UserRepository {
    override fun getUserById(id: Int): User {
        return [1].getUser(id)
    }
}
Drag options to blanks, or click blank then click option'
AremoteDataSource
BuserService
CuserDao
DlocalDataSource
Attempts:
3 left
💡 Hint
Common Mistakes
Using remoteDataSource instead of localDataSource.
Using undefined variables like userDao or userService.
3fill in blank
hard

Fix the error in the repository method to return a nullable User when not found.

Android Kotlin
override fun getUserById(id: Int): [1] {
    return localDataSource.getUser(id)
}
Drag options to blanks, or click blank then click option'
ABoolean
BUser?
CList<User>
DUser
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a non-nullable User type which causes errors if user is missing.
Returning a list or Boolean which does not match the method purpose.
4fill in blank
hard

Fill both blanks to create a repository method that saves a user and returns success status.

Android Kotlin
fun saveUser(user: User): [1] {
    val result = localDataSource.[2](user)
    return result > 0
}
Drag options to blanks, or click blank then click option'
ABoolean
BInt
CsaveUser
DinsertUser
Attempts:
3 left
💡 Hint
Common Mistakes
Returning Int instead of Boolean.
Calling a method named saveUser which does not exist in data source.
5fill in blank
hard

Fill all three blanks to define a repository method that fetches users filtered by age greater than a value.

Android Kotlin
fun getUsersOlderThan(age: Int): List<User> {
    return localDataSource.getUsers().filter { user -> user.[1] [2] [3] }
}
Drag options to blanks, or click blank then click option'
Aage
B>
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong property like name instead of age.
Using wrong comparison operators.