Challenge - 5 Problems
Override Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of overridden method call
What is the output of this Kotlin code when calling
greet() on an instance of Dog?Kotlin
open class Animal { open fun greet() = "Hello from Animal" } class Dog : Animal() { override fun greet() = "Woof from Dog" } fun main() { val pet: Animal = Dog() println(pet.greet()) }
Attempts:
2 left
💡 Hint
Remember that the
override keyword allows a subclass to provide its own implementation of a method.✗ Incorrect
The
Dog class overrides the greet() method from Animal. When calling greet() on a Dog instance, the overridden method in Dog runs, printing "Woof from Dog".❓ Predict Output
intermediate2:00remaining
Output when calling base and overridden methods
What will be printed when running this Kotlin code?
Kotlin
open class Vehicle { open fun start() = "Vehicle started" } class Car : Vehicle() { override fun start() = "Car started" fun startBase() = super.start() } fun main() { val myCar = Car() println(myCar.start()) println(myCar.startBase()) }
Attempts:
2 left
💡 Hint
The
super keyword calls the base class method even if it is overridden.✗ Incorrect
The
start() method in Car overrides the base method and returns "Car started". The startBase() method calls super.start(), which runs the base class method returning "Vehicle started".🔧 Debug
advanced2:00remaining
Identify the error in method overriding
Why does this Kotlin code fail to compile?
Kotlin
open class Shape { fun draw() = "Drawing shape" } class Circle : Shape() { override fun draw() = "Drawing circle" }
Attempts:
2 left
💡 Hint
In Kotlin, only methods marked
open can be overridden.✗ Incorrect
The
draw() method in Shape is not marked open, so it cannot be overridden in Circle. This causes a compilation error.📝 Syntax
advanced2:00remaining
Correct override syntax
Which option correctly overrides the
calculate() method in Kotlin?Kotlin
open class Calculator { open fun calculate(x: Int): Int = x * 2 } class AdvancedCalculator : Calculator() { // override method here }
Attempts:
2 left
💡 Hint
The
override keyword must come before fun and the parameter types must be specified.✗ Incorrect
Option C uses the correct syntax:
override fun calculate(x: Int): Int = x * 3. Other options have syntax errors such as wrong keyword order, missing fun, or missing parameter types.🚀 Application
expert3:00remaining
Determine output with multiple overrides and calls
What is the output of this Kotlin program?
Kotlin
open class A { open fun foo() = "A" fun bar() = foo() } class B : A() { override fun foo() = "B" } class C : B() { override fun foo() = "C" fun test() = super.foo() } fun main() { val c = C() println(c.foo()) println(c.bar()) println(c.test()) }
Attempts:
2 left
💡 Hint
Remember that
super refers to the immediate superclass, and method calls use the most derived override.✗ Incorrect
Calling
c.foo() runs C's override, printing "C". Calling c.bar() calls A's bar(), which calls foo() dynamically, so it runs C's foo(), printing "C". Calling c.test() calls super.foo() from C, which calls B's foo(), printing "B".