Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to delegate the interface implementation using the 'by' keyword.
Kotlin
interface Printer {
fun printMessage()
}
class ConsolePrinter : Printer {
override fun printMessage() {
println("Printing to console")
}
}
class Delegator(private val printer: Printer) : Printer [1] printer
fun main() {
val printer = ConsolePrinter()
val delegator = Delegator(printer)
delegator.printMessage()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'implements' or 'extends' which are Java keywords, not Kotlin delegation keywords.
Using ':' alone without 'by' does not delegate implementation.
✗ Incorrect
In Kotlin, the 'by' keyword is used for class delegation to delegate interface implementation to another object.
2fill in blank
mediumComplete the code to delegate the 'calculate' function to the CalculatorImpl class.
Kotlin
interface Calculator {
fun calculate(x: Int, y: Int): Int
}
class CalculatorImpl : Calculator {
override fun calculate(x: Int, y: Int): Int {
return x + y
}
}
class DelegatedCalculator(private val calc: Calculator) : Calculator [1] calc
fun main() {
val calculator = CalculatorImpl()
val delegated = DelegatedCalculator(calculator)
println(delegated.calculate(5, 3))
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Java keywords like 'implements' or 'extends' instead of Kotlin's 'by'.
Omitting the delegation keyword causes a compilation error.
✗ Incorrect
The 'by' keyword delegates the interface implementation to the provided object in Kotlin.
3fill in blank
hardFix the error in the delegation syntax to correctly delegate the 'speak' function.
Kotlin
interface Speaker {
fun speak()
}
class EnglishSpeaker : Speaker {
override fun speak() {
println("Hello")
}
}
class Person(private val speaker: Speaker) : Speaker [1] speaker
fun main() {
val englishSpeaker = EnglishSpeaker()
val person = Person(englishSpeaker)
person.speak()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Java keywords like 'implements' or 'extends' instead of 'by'.
Using ':' alone without 'by' causes syntax errors.
✗ Incorrect
The correct Kotlin syntax for delegation uses the 'by' keyword to delegate interface implementation.
4fill in blank
hardFill both blanks to delegate the 'draw' function to the 'ShapeImpl' class.
Kotlin
interface Shape {
fun draw()
}
class ShapeImpl : Shape {
override fun draw() {
println("Drawing shape")
}
}
class ShapeDelegate(private val shape: Shape) : Shape [1] [2]
fun main() {
val shapeImpl = ShapeImpl()
val shapeDelegate = ShapeDelegate(shapeImpl)
shapeDelegate.draw()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'implements' or 'extends' instead of 'by'.
Not specifying the object to delegate to.
✗ Incorrect
The 'by' keyword delegates the interface implementation to the object 'shape'.
5fill in blank
hardFill all three blanks to delegate the 'play' function to the 'MusicPlayerImpl' class and override it in the delegator.
Kotlin
interface MusicPlayer {
fun play()
}
class MusicPlayerImpl : MusicPlayer {
override fun play() {
println("Playing music")
}
}
class CustomPlayer(private val player: MusicPlayer) : MusicPlayer [1] [2] {
override fun play() {
println("Custom player start")
player.[3]()
println("Custom player end")
}
}
fun main() {
val impl = MusicPlayerImpl()
val custom = CustomPlayer(impl)
custom.play()
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'implements' instead of 'by'.
Forgetting to call the delegated function inside the override.
Using wrong variable or function names.
✗ Incorrect
Use 'by' to delegate, 'player' as the delegate object, and call 'play()' on it inside the override.