Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define an extension function named greet for the String class that prints a greeting message.
Kotlin
fun String.[1]() { println("Hello, $this!") } "World".greet()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name than the one called on the string.
Forgetting to define the function as an extension (missing 'String.' before the function name).
✗ Incorrect
The extension function is named 'greet', so the blank should be filled with 'greet'.
2fill in blank
mediumComplete the code to create a DSL builder function html that takes a lambda with receiver of type HtmlBuilder.
Kotlin
fun html(block: [1].() -> Unit) {
val builder = HtmlBuilder()
builder.block()
println(builder.result)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong receiver type like String or Int.
Not matching the receiver type with the builder instance.
✗ Incorrect
The lambda with receiver should be of type HtmlBuilder to allow building HTML inside the block.
3fill in blank
hardFix the error in the extension function that adds an li element inside a ul builder.
Kotlin
class UlBuilder { val items = mutableListOf<String>() fun li(text: String) { items.add(text) } } fun UlBuilder.[1](text: String) { li(text) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different function name that does not match the existing method.
Causing ambiguity by defining a different name for the same action.
✗ Incorrect
The extension function should have the same name 'li' to correctly add list items inside UlBuilder.
4fill in blank
hardFill both blanks to create a DSL function body inside HtmlBuilder that takes a lambda with receiver BodyBuilder.
Kotlin
class HtmlBuilder { var result = "" fun [1](block: [2].() -> Unit) { val bodyBuilder = BodyBuilder() bodyBuilder.block() result += bodyBuilder.content } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' or 'HeadBuilder' instead of 'body' and 'BodyBuilder'.
Mixing function name and receiver type.
✗ Incorrect
The function name is 'body' and the lambda receiver type is 'BodyBuilder' to build the body content.
5fill in blank
hardFill all three blanks to create a DSL function div inside BodyBuilder that takes a lambda with receiver DivBuilder and appends the result.
Kotlin
class BodyBuilder { var content = "" fun [1](block: [2].() -> Unit) { val divBuilder = [3]() divBuilder.block() content += divBuilder.html } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for function, receiver, or instance.
Confusing BodyBuilder with DivBuilder.
✗ Incorrect
The function name is 'div', the lambda receiver is 'DivBuilder', and the instance created is 'DivBuilder()'.