Challenge - 5 Problems
Let Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Kotlin code using let?
Consider the following Kotlin code snippet. What will it print when run?
Kotlin
val name: String? = "Anna" val result = name?.let { it.uppercase() } ?: "No name" println(result)
Attempts:
2 left
💡 Hint
Remember that let runs the block only if the variable is not null.
✗ Incorrect
The variable 'name' is not null, so let runs and converts it to uppercase, printing 'ANNA'.
❓ Predict Output
intermediate2:00remaining
What does this Kotlin code print when using let with a nullable variable?
Analyze this Kotlin code and determine its output.
Kotlin
val number: Int? = null val output = number?.let { it * 2 } ?: -1 println(output)
Attempts:
2 left
💡 Hint
If the variable is null, let block does not run and the Elvis operator provides the default.
✗ Incorrect
Since 'number' is null, let is skipped and the Elvis operator returns -1.
🔧 Debug
advanced2:00remaining
Why does this Kotlin code cause a compilation error?
Examine the code below and identify the reason for the compilation error.
Kotlin
val text: String? = "hello" val length = text.let { it.length } println(length)
Attempts:
2 left
💡 Hint
Consider the type of 'it' inside the let block when the receiver is nullable.
✗ Incorrect
Inside let, 'it' is the nullable type String?, so accessing 'length' without safe call or null check causes error.
🧠 Conceptual
advanced2:00remaining
What is a common use case for Kotlin's let function?
Choose the best description of a typical use case for the let function in Kotlin.
Attempts:
2 left
💡 Hint
Think about how let helps with null safety and chaining.
✗ Incorrect
Let is often used to run code only if a variable is not null and to transform or use its value safely.
❓ Predict Output
expert2:00remaining
What is the output of this Kotlin code using nested let functions?
Analyze the following Kotlin code and determine what it prints.
Kotlin
val a: String? = "foo" val b: String? = null val result = a?.let { x -> b?.let { y -> "$x and $y" } } ?: "No result" println(result)
Attempts:
2 left
💡 Hint
Remember that the inner let runs only if 'b' is not null.
✗ Incorrect
Since 'b' is null, the inner let does not run, so the whole expression is null and Elvis operator returns 'No result'.