Complete the code to print the length of the string using let.
val text = "hello" text.[1] { println(it.length) }
apply which uses this instead of it.also which returns the original object but is meant for side effects.The let function passes the object as it to the lambda, allowing us to access it.length.
Complete the code to initialize and print the length of the string using run.
val length = "Kotlin".[1] { length } println(length)
also which returns the original object, not the lambda result.let which uses it instead of this.run executes the lambda with this as the receiver and returns the lambda result, here the string length.
Fix the error in the lambda to correctly filter even numbers.
val numbers = listOf(1, 2, 3, 4, 5) val evens = numbers.filter { it [1] 2 == 0 } println(evens)
/ which performs division and returns a quotient.+ or * which are arithmetic but not for checking evenness.The modulo operator % returns the remainder. it % 2 == 0 checks if a number is even.
Fill both blanks to create a map of words to their lengths, filtering words longer than 3 characters.
val words = listOf("cat", "house", "dog", "elephant") val lengths = words.associateWith { [1] } val filtered = lengths.filter { it.value [2] 3 } println(filtered)
it.size which is not a string property.<= instead of >.associateWith maps each word to its length using it.length. Then filter keeps entries where length is greater than 3.
Fill all three blanks to create a map of uppercase words to their lengths, filtering lengths greater than 4.
val words = listOf("apple", "bat", "carrot", "dog") val result = words.associate { [1] to [2] } .filter { it.value [3] 4 } println(result)
it.toString() which returns the same string.< or <= instead of > for filtering.associate creates pairs of uppercase words and their lengths. Then filter keeps pairs with length greater than 4.