0
0
Kotlinprogramming~20 mins

Extensions for DSL building in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DSL Mastery in Kotlin
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin DSL extension code?

Consider the following Kotlin code that uses extension functions to build a simple DSL for HTML-like tags. What will be printed when this code runs?

Kotlin
class Tag(val name: String) {
    private val children = mutableListOf<Tag>()
    fun tag(name: String, init: Tag.() -> Unit) {
        val t = Tag(name)
        t.init()
        children.add(t)
    }
    override fun toString(): String {
        val inner = children.joinToString("") { it.toString() }
        return "<$name>$inner</$name>"
    }
}

fun html(init: Tag.() -> Unit): Tag {
    val root = Tag("html")
    root.init()
    return root
}

fun main() {
    val result = html {
        tag("body") {
            tag("p") {}
        }
    }
    println(result)
}
A<html><body><p></p></body></html>
B<html><body></body><p></p></html>
C<html><p></p><body></body></html>
DCompilation error due to missing return type
Attempts:
2 left
💡 Hint

Look at how the tag function adds children inside the current tag.

🧠 Conceptual
intermediate
1:30remaining
Which Kotlin feature allows building DSLs with extension functions?

In Kotlin, what feature primarily enables the creation of DSLs using extension functions?

AData classes with default parameters
BHigher-order functions with lambda receivers
CInline classes with operator overloading
DSealed classes with companion objects
Attempts:
2 left
💡 Hint

Think about how lambdas can be called with a receiver object.

🔧 Debug
advanced
2:00remaining
Why does this Kotlin DSL code throw a NullPointerException?

Examine the Kotlin DSL code below. It throws a NullPointerException at runtime. What is the cause?

Kotlin
class Builder {
    var text: String? = null
    fun build(): String {
        return text!!.toUpperCase()
    }
}

fun buildString(init: Builder.() -> Unit): String {
    val builder = Builder()
    builder.init()
    return builder.build()
}

fun main() {
    val result = buildString {
        // text is not set here
    }
    println(result)
}
AThe 'toUpperCase()' method is deprecated and causes an error
BThe 'buildString' function is missing a return statement
CThe 'init' lambda is not called on the Builder instance
DThe 'text' property is never initialized before calling 'build()'
Attempts:
2 left
💡 Hint

Check the value of 'text' before calling toUpperCase().

📝 Syntax
advanced
1:30remaining
Which option correctly defines an extension function for DSL building in Kotlin?

Which of the following Kotlin code snippets correctly defines an extension function named section for a class Document that takes a lambda with receiver?

Afun section(Document.init: () -> Unit) { init() }
Bfun Document.section(init: () -> Unit) { this.init() }
Cfun Document.section(init: Document.() -> Unit) { init() }
Dfun Document.section(init: Document) -> Unit { init() }
Attempts:
2 left
💡 Hint

Remember the syntax for extension functions with lambda receivers.

🚀 Application
expert
2:30remaining
How many tags are created by this Kotlin DSL code?

Given the Kotlin DSL code below, how many Tag instances are created in total?

Kotlin
class Tag(val name: String) {
    val children = mutableListOf<Tag>()
    fun tag(name: String, init: Tag.() -> Unit = {}) {
        val t = Tag(name)
        t.init()
        children.add(t)
    }
}

fun main() {
    val root = Tag("root")
    root.tag("a") {
        tag("b") {
            tag("c")
        }
        tag("d")
    }
    root.tag("e")
}
A6
B5
C4
D7
Attempts:
2 left
💡 Hint

Count the root and all nested tags created by each tag call.