Complete the code to define a lambda with receiver that returns the length of the string.
val lengthLambda: String.() -> Int = { [1] }length() which is not a function in Kotlin String.this and just writing length which is not recognized.In Kotlin, inside a lambda with receiver, this refers to the receiver object. To get the length of the string, use this.length.
Complete the code to call the lambda with receiver on the string "Hello".
val result = "Hello".[1](lengthLambda)
apply which returns the object, not the lambda result.let which uses lambda with parameter, not receiver.The run function calls the lambda with receiver on the object and returns the lambda result.
Fix the error in the lambda with receiver that tries to convert the string to uppercase.
val upperLambda: String.() -> String = { [1] }toUpperCase.this which can cause confusion.Inside the lambda with receiver, call the function with parentheses and use this to refer to the string.
Fill both blanks to create a lambda with receiver that repeats the string twice and checks if its length is greater than 5.
val checkLambda: String.() -> Boolean = { val repeated = this [1] 2; repeated.length [2] 5 }* which is not valid for strings in Kotlin.< instead of > for comparison.Use + to repeat the string twice (concatenate it with itself), and > to check if length is greater than 5.
Fill all three blanks to create a map from strings to their uppercase versions only if the string length is less than 4.
val words = listOf("cat", "dog", "elephant") val result = words.associateWith { it.[1]() } .filter { it.key.length [2] 4 } .mapValues { it.value.[3]() }
length as a function call instead of property.toUpperCase and toLowerCase in wrong places.First, convert each word to uppercase with toUpperCase(). Then filter words with length less than 4 using <. Finally, convert the values to lowercase with toLowerCase().