Bird
Raised Fist0
Kotlinprogramming~20 mins

Infix functions in DSLs in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Infix DSL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of infix function call in Kotlin DSL
What is the output of this Kotlin code using an infix function in a DSL style?
Kotlin
class Builder {
    private val items = mutableListOf<String>()
    infix fun add(item: String) {
        items.add(item)
    }
    fun build() = items.joinToString(", ")
}

fun main() {
    val builder = Builder()
    builder add "apple"
    builder add "banana"
    println(builder.build())
}
A[apple, banana]
Bapple, banana
Capple banana
Dapple; banana
Attempts:
2 left
💡 Hint
Remember that the build function joins items with a comma and space.
❓ Predict Output
intermediate
2:00remaining
Result of chained infix calls in Kotlin DSL
What is the output of this Kotlin code with chained infix function calls?
Kotlin
class SentenceBuilder {
    private val words = mutableListOf<String>()
    infix fun add(word: String): SentenceBuilder {
        words.add(word)
        return this
    }
    fun build() = words.joinToString(" ")
}

fun main() {
    val sentence = SentenceBuilder() add "Hello" add "world" add "!"
    println(sentence.build())
}
AHello world !
BHello, world, !
CHello world!
DHello world
Attempts:
2 left
💡 Hint
Check how the words are joined in the build function.
🔧 Debug
advanced
2:00remaining
Identify the error in this infix function usage
What error does this Kotlin code produce when run?
Kotlin
class Config {
    infix fun set(key: String) {
        println("Setting $key")
    }
}

fun main() {
    val config = Config()
    config set "mode" to "dark"
}
AUnresolved reference: to
BNo error, prints 'Setting mode to dark'
CSyntaxError: Unexpected token 'to'
DTypeError: 'to' is not a function
Attempts:
2 left
💡 Hint
Check if 'to' is defined or imported in this context.
🧠 Conceptual
advanced
1:30remaining
Understanding infix function constraints in Kotlin DSLs
Which of the following is NOT a requirement for a function to be used as an infix function in Kotlin?
AIt must be a member function or an extension function
BIt must have exactly one parameter
CIt must return Unit
DIt must be marked with the 'infix' keyword
Attempts:
2 left
💡 Hint
Think about the return type flexibility of infix functions.
❓ Predict Output
expert
3:00remaining
Output of nested infix function calls in Kotlin DSL
What is the output of this Kotlin code using nested infix functions in a DSL style?
Kotlin
class Node(val name: String) {
    private val children = mutableListOf<Node>()
    infix fun add(child: Node): Node {
        children.add(child)
        return this
    }
    fun printTree(indent: String = ""): String {
        val current = "$indent$name"
        val childStrings = children.joinToString("\n") { it.printTree(indent + "  ") }
        return if (children.isEmpty()) current else "$current\n$childStrings"
    }
}

fun main() {
    val root = Node("root") add Node("child1") add Node("child2") add Node("child3")
    println(root.printTree())
}
A
root
  child1
    child2
  child3
Broot child1 child2 child3
C
root
  child1
    child2
      child3
D
root
  child1
  child2
  child3
Attempts:
2 left
💡 Hint
Consider how the infix 'add' returns 'this' and how chaining affects the tree structure.

Practice

(1/5)
1. What is the main benefit of using infix functions in Kotlin DSLs?
easy
A. They allow calling functions without dots or parentheses, making code more readable.
B. They improve the performance of the program.
C. They enable functions to have multiple return types.
D. They automatically generate UI components.

Solution

  1. Step 1: Understand the purpose of infix functions

    Infix functions let you call a function with one argument without using dots or parentheses, making code look cleaner.
  2. Step 2: Identify the benefit in DSLs

    This style makes domain-specific languages (DSLs) easier to read and write, resembling natural language.
  3. Final Answer:

    They allow calling functions without dots or parentheses, making code more readable. -> Option A
  4. Quick Check:

    Infix functions improve readability = C [OK]
Hint: Infix means no dots or parentheses for one-argument calls [OK]
Common Mistakes:
  • Thinking infix improves performance
  • Believing infix allows multiple return types
  • Assuming infix auto-generates UI
2. Which of the following is the correct syntax to declare an infix function in Kotlin?
easy
A. infix fun add(x: Int) { }
B. fun infix add(x: Int) { }
C. fun add infix(x: Int) { }
D. fun add(x: Int) infix { }

Solution

  1. Step 1: Recall infix function declaration rules

    In Kotlin, the keyword infix must appear before fun when declaring an infix function.
  2. Step 2: Match the correct syntax

    infix fun add(x: Int) { } correctly places infix before fun and declares the function with one parameter.
  3. Final Answer:

    infix fun add(x: Int) { } -> Option A
  4. Quick Check:

    Keyword order: infix before fun = A [OK]
Hint: Remember: 'infix' keyword comes before 'fun' in declaration [OK]
Common Mistakes:
  • Placing 'infix' after function name
  • Putting 'infix' after parameter list
  • Omitting 'infix' keyword entirely
3. What will be the output of the following Kotlin code?
class Builder {
    infix fun add(value: String) {
        println("Added: $value")
    }
}

fun main() {
    val b = Builder()
    b add "Kotlin"
}
medium
A. No output
B. b add Kotlin
C. Syntax error
D. Added: Kotlin

Solution

  1. Step 1: Understand infix function call

    The infix function add is called on object b with argument "Kotlin" without dots or parentheses.
  2. Step 2: Check the function behavior

    The function prints "Added: Kotlin" to the console.
  3. Final Answer:

    Added: Kotlin -> Option D
  4. Quick Check:

    Infix call prints 'Added: Kotlin' = A [OK]
Hint: Infix calls print inside function as expected [OK]
Common Mistakes:
  • Expecting syntax error due to missing parentheses
  • Thinking it prints the call expression
  • Assuming no output without explicit print
4. Identify the error in this Kotlin code using an infix function:
class Config {
    infix fun set(key: String, value: String) {
        println("$key = $value")
    }
}

fun main() {
    val c = Config()
    c set ("color", "blue")
}
medium
A. Missing return type declaration.
B. Infix functions cannot have more than one parameter.
C. The function name 'set' is reserved and cannot be infix.
D. Parentheses are not allowed in infix calls.

Solution

  1. Step 1: Check infix function parameter count

    Kotlin infix functions must have exactly one parameter, but here set has two parameters.
  2. Step 2: Understand the error cause

    This causes a compilation error because infix functions cannot have multiple parameters.
  3. Final Answer:

    Infix functions cannot have more than one parameter. -> Option B
  4. Quick Check:

    One parameter only for infix = D [OK]
Hint: Infix functions accept exactly one parameter [OK]
Common Mistakes:
  • Trying to use multiple parameters in infix
  • Assuming parentheses are forbidden in all calls
  • Thinking 'set' is a reserved infix name
5. Given the following Kotlin DSL code, what will be the output?
class HtmlTag(val name: String) {
    private val children = mutableListOf<HtmlTag>()
    infix fun add(child: HtmlTag) {
        children.add(child)
    }
    fun render(): String {
        val inner = children.joinToString("") { it.render() }
        return "<$name>$inner</$name>"
    }
}

fun main() {
    val html = HtmlTag("html")
    val body = HtmlTag("body")
    val p = HtmlTag("p")
    html add body
    body add p
    println(html.render())
}
hard
A.

B.

C.

D. Compilation error due to infix usage

Solution

  1. Step 1: Understand the infix add function

    The infix function add adds child tags to the parent's children list, building a tree structure.
  2. Step 2: Trace the tag hierarchy and rendering

    html adds body, body adds p, so the structure is html > body > p. The render function outputs nested tags accordingly.
  3. Final Answer:

    <html><body><p></p></body></html> -> Option C
  4. Quick Check:

    Nested tags render correctly = B [OK]
Hint: Infix builds nested structure; render outputs nested tags [OK]
Common Mistakes:
  • Mixing tag closing order
  • Assuming infix causes compile error
  • Ignoring nested children in render
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard