0
0
Kotlinprogramming~10 mins

Testing scope functions and lambdas in 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 print the length of the string using let.

Kotlin
val text = "hello"
text.[1] {
    println(it.length)
}
Drag options to blanks, or click blank then click option'
Aapply
Brun
Clet
Dalso
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply which uses this instead of it.
Using also which returns the original object but is meant for side effects.
2fill in blank
medium

Complete the code to initialize and print the length of the string using run.

Kotlin
val length = "Kotlin".[1] {
    length
}
println(length)
Drag options to blanks, or click blank then click option'
Aalso
Blet
Cwith
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using also which returns the original object, not the lambda result.
Using let which uses it instead of this.
3fill in blank
hard

Fix the error in the lambda to correctly filter even numbers.

Kotlin
val numbers = listOf(1, 2, 3, 4, 5)
val evens = numbers.filter { it [1] 2 == 0 }
println(evens)
Drag options to blanks, or click blank then click option'
A+
B%
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using / which performs division and returns a quotient.
Using + or * which are arithmetic but not for checking evenness.
4fill in blank
hard

Fill both blanks to create a map of words to their lengths, filtering words longer than 3 characters.

Kotlin
val words = listOf("cat", "house", "dog", "elephant")
val lengths = words.associateWith { [1] }
val filtered = lengths.filter { it.value [2] 3 }
println(filtered)
Drag options to blanks, or click blank then click option'
Ait.length
B>
C<=
Dit.size
Attempts:
3 left
💡 Hint
Common Mistakes
Using it.size which is not a string property.
Using wrong comparison operators like <= instead of >.
5fill in blank
hard

Fill all three blanks to create a map of uppercase words to their lengths, filtering lengths greater than 4.

Kotlin
val words = listOf("apple", "bat", "carrot", "dog")
val result = words.associate { [1] to [2] }
    .filter { it.value [3] 4 }
println(result)
Drag options to blanks, or click blank then click option'
Ait.uppercase()
Bit.length
C>
Dit.toString()
Attempts:
3 left
💡 Hint
Common Mistakes
Using it.toString() which returns the same string.
Using < or <= instead of > for filtering.