0
0
Kotlinprogramming~20 mins

Delegation vs inheritance decision in Kotlin - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Delegation vs Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of delegation example
What is the output of this Kotlin code using delegation?
Kotlin
interface Printer {
    fun print()
}

class RealPrinter : Printer {
    override fun print() {
        println("Printing from RealPrinter")
    }
}

class Delegator(printer: Printer) : Printer by printer

fun main() {
    val realPrinter = RealPrinter()
    val delegator = Delegator(realPrinter)
    delegator.print()
}
APrinting from RealPrinter
BRuntime exception
CCompilation error
DPrinting from Delegator
Attempts:
2 left
💡 Hint
Delegation forwards calls to the provided object.
Predict Output
intermediate
2:00remaining
Output of inheritance example
What is the output of this Kotlin code using inheritance?
Kotlin
open class Printer {
    open fun print() {
        println("Printing from Printer")
    }
}

class ChildPrinter : Printer() {
    override fun print() {
        println("Printing from ChildPrinter")
    }
}

fun main() {
    val printer: Printer = ChildPrinter()
    printer.print()
}
APrinting from Printer
BCompilation error
CRuntime exception
DPrinting from ChildPrinter
Attempts:
2 left
💡 Hint
Inheritance allows overriding methods.
🧠 Conceptual
advanced
2:00remaining
Choosing delegation over inheritance
Which scenario best justifies using delegation instead of inheritance in Kotlin?
AWhen you want to reuse code but avoid tight coupling and allow changing behavior at runtime
BWhen you want to create a strict type hierarchy with shared implementation
CWhen you want to override a method in a subclass to change behavior
DWhen you want to prevent any changes to the base class behavior
Attempts:
2 left
💡 Hint
Delegation allows flexible behavior without inheritance constraints.
Predict Output
advanced
2:00remaining
Output with delegation and overridden method
What is the output of this Kotlin code where delegation is combined with an overridden method?
Kotlin
interface Speaker {
    fun speak()
}

class EnglishSpeaker : Speaker {
    override fun speak() {
        println("Hello")
    }
}

class CustomSpeaker(private val speaker: Speaker) : Speaker by speaker {
    override fun speak() {
        println("Custom speak start")
        speaker.speak()
        println("Custom speak end")
    }
}

fun main() {
    val english = EnglishSpeaker()
    val custom = CustomSpeaker(english)
    custom.speak()
}
ACompilation error
B
Custom speak start
Hello
Custom speak end
C
Custom speak start
Custom speak end
DHello
Attempts:
2 left
💡 Hint
Overriding a delegated method allows adding behavior before and after delegation.
🧠 Conceptual
expert
3:00remaining
Why avoid inheritance for behavior reuse?
Why might inheritance be a poor choice for reusing behavior compared to delegation in Kotlin?
AInheritance cannot override methods, delegation can
BInheritance is slower at runtime than delegation
CInheritance creates tight coupling and limits flexibility, while delegation allows composition and runtime behavior changes
DInheritance requires more code to implement than delegation
Attempts:
2 left
💡 Hint
Think about coupling and flexibility differences.