0
0
Android Kotlinmobile~10 mins

Derived state 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 create a derived state that holds the length of the text.

Android Kotlin
val text = remember { mutableStateOf("") }
val textLength = remember [1] {
    text.value.length
}
Drag options to blanks, or click blank then click option'
AmutableStateOf
BderivedStateOf
CrememberUpdatedState
DstateOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using mutableStateOf instead of derivedStateOf causes the length not to update automatically.
Forgetting to use remember causes recomposition issues.
2fill in blank
medium

Complete the code to create a derived state that returns true if the text length is greater than 5.

Android Kotlin
val text = remember { mutableStateOf("") }
val isLongText = remember [1] {
    text.value.length > 5
}
Drag options to blanks, or click blank then click option'
AderivedStateOf
BmutableStateOf
CrememberUpdatedState
DstateOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using mutableStateOf will not update automatically when text.value changes.
Using rememberUpdatedState is not suitable for derived state.
3fill in blank
hard

Fix the error in the code by completing the blank to create a derived state that returns the uppercase version of the text.

Android Kotlin
val text = remember { mutableStateOf("") }
val upperText = remember [1] {
    text.value.uppercase()
}
Drag options to blanks, or click blank then click option'
AderivedStateOf
BmutableStateOf
CrememberUpdatedState
DstateOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using mutableStateOf causes the derived state not to update automatically.
Using rememberUpdatedState is not appropriate here.
4fill in blank
hard

Fill both blanks to create a derived state that returns true if the text contains the letter 'a' and its length is more than 3.

Android Kotlin
val text = remember { mutableStateOf("") }
val condition = remember [1] {
    text.value.contains('a') && text.value.length [2] 3
}
Drag options to blanks, or click blank then click option'
AderivedStateOf
B>
C<
DmutableStateOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using mutableStateOf instead of derivedStateOf.
Using the less than operator instead of greater than.
5fill in blank
hard

Fill all three blanks to create a derived state that returns a map of words to their lengths, but only for words longer than 4 characters.

Android Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val wordLengths = remember [1] {
    words.filter { it.length [2] 4 }
        .associateWith { it.length [3] }
}
Drag options to blanks, or click blank then click option'
AderivedStateOf
B>
Ctrue
DmutableStateOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using mutableStateOf instead of derivedStateOf.
Using less than operator instead of greater than.
Leaving the third blank empty or incorrect.