Challenge - 5 Problems
Lambda Receiver Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of lambda with receiver modifying a StringBuilder
What is the output of this Kotlin code using a lambda with receiver on a StringBuilder?
Kotlin
val result = buildString { append("Hello") append(", ") append("World") } println(result)
Attempts:
2 left
💡 Hint
Remember that buildString uses a lambda with receiver on StringBuilder to append text.
✗ Incorrect
The buildString function takes a lambda with receiver where the receiver is a StringBuilder. The append calls add text to the builder, resulting in "Hello, World".
❓ Predict Output
intermediate2:00remaining
Result of applying lambda with receiver on a mutable list
What will be printed by this Kotlin code using a lambda with receiver on a MutableList?
Kotlin
val list = mutableListOf(1, 2, 3) list.apply { add(4) remove(2) } println(list)
Attempts:
2 left
💡 Hint
The apply function uses a lambda with receiver and returns the receiver after applying changes.
✗ Incorrect
The apply lambda adds 4 and removes 2 from the list. So the list becomes [1, 3, 4].
🔧 Debug
advanced2:00remaining
Identify the error in this lambda with receiver usage
What error does this Kotlin code produce?
Kotlin
fun buildMessage(block: StringBuilder.() -> Unit): String { val sb = StringBuilder() block(sb) return sb.toString() } val message = buildMessage { append("Hi") append(123) } println(message)
Attempts:
2 left
💡 Hint
Check if StringBuilder has an append method that accepts Int.
✗ Incorrect
StringBuilder has an append method overloaded for Int, so append(123) works fine. The code runs without error and prints "Hi123".
📝 Syntax
advanced2:00remaining
Which option correctly defines a lambda with receiver type?
Which of these Kotlin declarations correctly defines a lambda with receiver that takes no parameters and returns Unit?
Attempts:
2 left
💡 Hint
A lambda with receiver type uses the syntax ReceiverType.() -> ReturnType.
✗ Incorrect
Option A correctly declares a lambda with receiver on String that returns Unit. Option A is a normal function type, C returns String but uses this incorrectly, D is invalid syntax.
🚀 Application
expert3:00remaining
Using lambda with receiver to build a custom HTML tag
Given this Kotlin code, what is the output of the program?
Kotlin
class Tag(val name: String) { private val children = mutableListOf<Tag>() private var text: String = "" fun text(value: String) { text = value } fun tag(name: String, block: Tag.() -> Unit) { val t = Tag(name) t.block() children.add(t) } override fun toString(): String { val builder = StringBuilder() builder.append("<$name>") if (text.isNotEmpty()) builder.append(text) for (c in children) builder.append(c.toString()) builder.append("</$name>") return builder.toString() } } fun html(block: Tag.() -> Unit): Tag { val root = Tag("html") root.block() return root } fun main() { val page = html { tag("body") { tag("h1") { text("Welcome") } tag("p") { text("This is a lambda with receiver example.") } } } println(page) }
Attempts:
2 left
💡 Hint
Look carefully at how children tags are added and how text is appended inside tags.
✗ Incorrect
The html function creates a root Tag "html". Inside, a "body" tag is added with two children: "h1" with text "Welcome" and "p" with the given text. The toString method builds nested tags correctly.