Challenge - 5 Problems
Kotlin Generics Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of generic function with upper bound
What is the output of this Kotlin code snippet?
Kotlin
open class Animal(val name: String) { fun sound() = "Some sound" } class Dog(name: String) : Animal(name) { fun bark() = "Woof" } fun <T : Animal> makeSound(animal: T): String { return animal.sound() } fun main() { val dog = Dog("Buddy") println(makeSound(dog)) }
Attempts:
2 left
💡 Hint
Think about which method is called inside makeSound and what is available in the upper bound type.
✗ Incorrect
The function makeSound accepts any type T that extends Animal. Inside, it calls animal.sound(), which is defined in Animal. Although Dog has bark(), makeSound only calls sound(), so output is "Some sound".
❓ Predict Output
intermediate2:00remaining
Result of generic function with multiple upper bounds
What will this Kotlin code print?
Kotlin
interface Runner { fun run() = "Running" } open class Animal(val name: String) class Dog(name: String) : Animal(name), Runner fun <T> action(animal: T) where T : Animal, T : Runner { println(animal.run()) } fun main() { val dog = Dog("Max") action(dog) }
Attempts:
2 left
💡 Hint
Check how multiple upper bounds allow calling methods from both types.
✗ Incorrect
The generic function action requires T to be both Animal and Runner. Dog satisfies both. The call animal.run() uses Runner's method, so output is "Running".
🔧 Debug
advanced2:00remaining
Identify the compilation error in generic function
Why does this Kotlin code fail to compile?
Kotlin
open class Animal class Dog : Animal() fun <T> printName(animal: T) where T : Animal { println(animal.name) } fun main() { val dog = Dog() printName(dog) }
Attempts:
2 left
💡 Hint
Check if the property 'name' exists in the upper bound type Animal.
✗ Incorrect
The Animal class does not have a 'name' property, so accessing animal.name causes a compilation error.
📝 Syntax
advanced2:00remaining
Correct syntax for generic function with upper bound
Which option shows the correct syntax for a generic function with an upper bound Animal?
Attempts:
2 left
💡 Hint
Recall the syntax for declaring upper bounds in Kotlin generics.
✗ Incorrect
Option A correctly uses the syntax to specify that T must be a subtype of Animal.
🚀 Application
expert3:00remaining
Determine the output of complex generic constraints
What is the output of this Kotlin program?
Kotlin
open class Vehicle(val brand: String) { open fun info() = "Vehicle brand: $brand" } interface Electric { fun batteryInfo() = "Battery full" } class Tesla(brand: String) : Vehicle(brand), Electric { override fun info() = "Tesla brand: $brand" } fun <T> printDetails(item: T) where T : Vehicle, T : Electric { println(item.info()) println(item.batteryInfo()) } fun main() { val myCar = Tesla("Model S") printDetails(myCar) }
Attempts:
2 left
💡 Hint
Check which info() method is called and how multiple upper bounds work.
✗ Incorrect
The generic function requires T to be Vehicle and Electric. Tesla overrides info(), so item.info() calls Tesla's version. batteryInfo() comes from Electric. Output is two lines: Tesla brand: Model S and Battery full.