Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a simple DSL function that prints a greeting.
Kotlin
fun greet() {
println([1])
}
greet() Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the greeting text.
Passing a function name instead of a string.
✗ Incorrect
The println function needs a string argument, so the greeting must be in quotes.
2fill in blank
mediumComplete the code to create a DSL builder function that adds two numbers.
Kotlin
fun addNumbers(block: () -> Int): Int {
return [1]()
}
val result = addNumbers { 5 + 3 }
println(result) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to return the function name without calling it.
Using println instead of calling the block.
✗ Incorrect
The block parameter is a function that returns an Int, so calling block() executes it.
3fill in blank
hardFix the error in the DSL function that builds a greeting message.
Kotlin
fun greeting(block: GreetingBuilder.() -> Unit): String {
val builder = GreetingBuilder()
builder.[1](block)
return builder.message
}
class GreetingBuilder {
var message = ""
fun sayHello() {
message = "Hello from DSL!"
}
}
println(greeting { sayHello() }) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using run which returns the block result instead of the object.
Trying to call invoke or call which are not applicable here.
✗ Incorrect
The apply function runs the block with the receiver and returns the receiver object.
4fill in blank
hardFill both blanks to create a DSL that builds a list of strings with a custom builder.
Kotlin
class StringListBuilder { val items = mutableListOf<String>() fun add(item: String) { items.[1](item) } } fun buildList(block: StringListBuilder.() -> Unit): List<String> { val builder = StringListBuilder() builder.[2](block) return builder.items } val list = buildList { add("apple") add("banana") } println(list)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove instead of add to add items.
Using run instead of apply which returns the object.
✗ Incorrect
The add function adds an item to the list, and apply runs the block on the builder object.
5fill in blank
hardFill all three blanks to create a DSL that builds a map with keys and values.
Kotlin
class MapBuilder { val map = mutableMapOf<String, Int>() fun put(key: String, value: Int) { map.[1](key, value) } } fun buildMap(block: MapBuilder.() -> Unit): Map<String, Int> { val builder = MapBuilder() builder.[2](block) return builder.[3] } val result = buildMap { put("one", 1) put("two", 2) } println(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using run instead of apply which returns the builder.
Returning the builder instead of the map property.
Using add instead of put for the map.
✗ Incorrect
The put method adds entries to the map, apply runs the block on the builder, and map returns the built map.