0
0
Kotlinprogramming~10 mins

Why immutability by default matters in Kotlin - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare an immutable variable in Kotlin.

Kotlin
val number = [1]
Drag options to blanks, or click blank then click option'
Avar
B10
Cmutable
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' instead of a value for assignment.
Trying to assign 'val' as a value.
2fill in blank
medium

Complete the code to declare a mutable variable in Kotlin.

Kotlin
var count = [1]
Drag options to blanks, or click blank then click option'
A5
Bval
Cvar
Dimmutable
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' as the value instead of a number.
Using 'val' as the value.
3fill in blank
hard

Fix the error in the code by choosing the correct keyword for immutability.

Kotlin
[1] message = "Hello"
Drag options to blanks, or click blank then click option'
Avar
Bmutable
Cconst
Dval
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'var' which allows the variable to be changed.
Using 'const' which is not valid here.
4fill in blank
hard

Fill both blanks to create a list that cannot be changed and a list that can be changed.

Kotlin
val immutableList = listOf(1, 2, 3)
[2] mutableList = [1](1, 2, 3)

mutableList = listOf(4, 5, 6)
Drag options to blanks, or click blank then click option'
AmutableListOf
Bval
Cvar
DlistOf
Attempts:
3 left
💡 Hint
Common Mistakes
Using listOf for mutable list.
Using val for mutable list variable.
5fill in blank
hard

Fill all three blanks to create a map with immutable keys and values, filtering only entries with values greater than 10.

Kotlin
val data = mapOf("a" to 5, "b" to 15, "c" to 20)
val filtered = data.filter { ([1], [2]) -> [3] > 10 }
Drag options to blanks, or click blank then click option'
Akey
Bvalue
Dk
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for key and value.
Using incorrect variable names that don't match the lambda destructuring.