Complete the code to use the let function to print the length of the string.
val name: String? = "Kotlin" name?.[1] { println("Length: ${it.length}") }
The let function is used here to execute the block only if name is not null, and it refers to the non-null value.
Complete the code to safely convert a nullable string to uppercase using let.
val input: String? = "hello" val result = input?.[1] { it.uppercase() } ?: "DEFAULT"
let is used to safely call uppercase() on the non-null input. If input is null, it returns "DEFAULT".
Fix the error in the code by choosing the correct function to chain calls and return the last expression.
val number = 5 val result = number.[1] { println("Number is $it") it * 2 } println(result)
let returns the result of the last expression in the block, so result will be 10. also returns the original object, not the block result.
Fill both blanks to create a map of words to their lengths, but only include words longer than 3 characters.
val words = listOf("cat", "house", "dog", "elephant") val lengths = words.associateWith { it.[1] } .filter { it.value [2] 3 }
length returns the length of the string. The filter keeps entries where the length is greater than 3.
Fill all three blanks to create a map of uppercase words to their lengths, including only words longer than 4 characters.
val words = listOf("apple", "bat", "carrot", "dog") val result = words.filter { it.length [1] 4 } .associateBy({ it.[2]() }, { it.[3] })
The filter keeps words longer than 4 characters. associateBy creates a map with uppercase words as keys and their lengths as values.