0
0
Kotlinprogramming~10 mins

Lambda with receiver concept 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 define a lambda with receiver that returns the length of the string.

Kotlin
val lengthLambda: String.() -> Int = { [1] }
Drag options to blanks, or click blank then click option'
Athis.length
Blength()
Clength
Dthis.length()
Attempts:
3 left
💡 Hint
Common Mistakes
Using length() which is not a function in Kotlin String.
Omitting this and just writing length which is not recognized.
2fill in blank
medium

Complete the code to call the lambda with receiver on the string "Hello".

Kotlin
val result = "Hello".[1](lengthLambda)
Drag options to blanks, or click blank then click option'
Awith
Blet
Crun
Dapply
Attempts:
3 left
💡 Hint
Common Mistakes
Using apply which returns the object, not the lambda result.
Using let which uses lambda with parameter, not receiver.
3fill in blank
hard

Fix the error in the lambda with receiver that tries to convert the string to uppercase.

Kotlin
val upperLambda: String.() -> String = { [1] }
Drag options to blanks, or click blank then click option'
Athis.toUpperCase()
BtoUpperCase()
CtoUpperCase
Dthis.toUpperCase
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses when calling toUpperCase.
Not using this which can cause confusion.
4fill in blank
hard

Fill both blanks to create a lambda with receiver that repeats the string twice and checks if its length is greater than 5.

Kotlin
val checkLambda: String.() -> Boolean = { val repeated = this [1] 2; repeated.length [2] 5 }
Drag options to blanks, or click blank then click option'
A*
B+
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using * which is not valid for strings in Kotlin.
Using < instead of > for comparison.
5fill in blank
hard

Fill all three blanks to create a map from strings to their uppercase versions only if the string length is less than 4.

Kotlin
val words = listOf("cat", "dog", "elephant")
val result = words.associateWith { it.[1]() }
    .filter { it.key.length [2] 4 }
    .mapValues { it.value.[3]() }
Drag options to blanks, or click blank then click option'
AtoUpperCase
B<
CtoLowerCase
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using length as a function call instead of property.
Mixing up toUpperCase and toLowerCase in wrong places.