0
0
Kotlinprogramming~20 mins

Configuration DSL pattern in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin DSL Master
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 configuration?
Consider this Kotlin DSL code that configures a server. What will be printed when running the main function?
Kotlin
class ServerConfig {
    var host: String = "localhost"
    var port: Int = 80

    fun printConfig() {
        println("Server running on $host:$port")
    }
}

fun server(block: ServerConfig.() -> Unit): ServerConfig {
    val config = ServerConfig()
    config.block()
    return config
}

fun main() {
    val config = server {
        host = "192.168.1.1"
        port = 8080
    }
    config.printConfig()
}
AServer running on localhost:80
BServer running on 192.168.1.1:80
CServer running on 192.168.1.1:8080
DServer running on localhost:8080
Attempts:
2 left
💡 Hint
Look at how the DSL block changes the properties inside ServerConfig.
🧠 Conceptual
intermediate
1:30remaining
Which Kotlin feature enables the Configuration DSL pattern?
Which Kotlin language feature is primarily used to create a Configuration DSL pattern like the example below? ```kotlin fun server(block: ServerConfig.() -> Unit): ServerConfig { val config = ServerConfig() config.block() return config } ```
AExtension functions with lambda receiver
BData classes with default parameters
CCompanion objects with factory methods
DSealed classes with inheritance
Attempts:
2 left
💡 Hint
Think about how the lambda can access ServerConfig's members directly inside the block.
🔧 Debug
advanced
2:30remaining
Why does this Kotlin DSL configuration fail to set the port?
Given this code, why does the port remain 80 instead of changing to 9090? ```kotlin class ServerConfig { var host: String = "localhost" var port: Int = 80 } fun server(block: ServerConfig.() -> Unit): ServerConfig { val config = ServerConfig() block() return config } fun main() { val config = server { host = "example.com" port = 9090 } println("${config.host}:${config.port}") } ```
AThe host property is set but port is shadowed by a local variable.
BThe port property is immutable and cannot be changed.
CThe server function returns a new ServerConfig ignoring the block changes.
DThe block() call is not invoked on the config instance, so it modifies nothing.
Attempts:
2 left
💡 Hint
Check how the block lambda is called inside the server function.
📝 Syntax
advanced
1:30remaining
Which option correctly defines a Kotlin DSL function with a lambda receiver?
Select the correct Kotlin function signature for a DSL function that takes a lambda with receiver of type Config and returns Config.
Afun configure(block: Config -> Unit): Config
Bfun configure(block: Config.() -> Unit): Config
Cfun configure(block: (Config) -> Unit): Config
Dfun configure(block: () -> Config): Config
Attempts:
2 left
💡 Hint
Look for the syntax that declares a lambda with receiver.
🚀 Application
expert
3:00remaining
How many items are in the resulting map after this Kotlin DSL runs?
Consider this Kotlin code using a DSL to build a map: ```kotlin class MapBuilder { private val map = mutableMapOf() fun entry(key: String, value: Int) { map[key] = value } fun build(): Map = map } fun map(block: MapBuilder.() -> Unit): Map { val builder = MapBuilder() builder.block() return builder.build() } fun main() { val result = map { entry("a", 1) entry("b", 2) entry("a", 3) } println(result.size) } ```
A2
B3
C1
D0
Attempts:
2 left
💡 Hint
Think about what happens when the same key is added twice in a map.