0
0
Kotlinprogramming~10 mins

Configuration DSL pattern in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a simple DSL function named config.

Kotlin
fun [1](block: Config.() -> Unit): Config {
    val config = Config()
    config.block()
    return config
}
Drag options to blanks, or click blank then click option'
Aconfig
Bconfigure
Csetup
DbuildConfig
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that is too generic or unrelated to configuration.
2fill in blank
medium

Complete the code to declare a mutable property host inside the Config class.

Kotlin
class Config {
    var [1]: String = ""
}
Drag options to blanks, or click blank then click option'
Aport
Bhost
Curl
Daddress
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated property names like port or url here.
3fill in blank
hard

Fix the error in the DSL usage by completing the code to set the port property inside the config block.

Kotlin
val conf = config {
    port = [1]
}
Drag options to blanks, or click blank then click option'
A"8080"
Bport
C8080
Dconfig.port
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning port as a string instead of an integer.
4fill in blank
hard

Fill both blanks to define the Config class with host and port properties.

Kotlin
class Config {
    var [1]: String = ""
    var [2]: Int = 0
}
Drag options to blanks, or click blank then click option'
Ahost
Burl
Cport
Daddress
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing property names or types incorrectly.
5fill in blank
hard

Fill all three blanks to create a DSL function config that returns a configured Config object with host and port properties.

Kotlin
fun [1](block: Config.() -> Unit): Config {
    val config = [2]()
    config.[3]()
    return config
}
Drag options to blanks, or click blank then click option'
Aconfig
BConfig
Cblock
Dconfigure
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong function or variable names, or forgetting to call the lambda.