0
0
Kotlinprogramming~20 mins

Type constraints with upper bounds in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Kotlin Generics Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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))
}
AWoof
BRuntime error
CSome sound
DCompilation error due to type constraint
Attempts:
2 left
💡 Hint
Think about which method is called inside makeSound and what is available in the upper bound type.
Predict Output
intermediate
2: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)
}
AAnimal's name printed
BCompilation error due to multiple upper bounds
CRuntime exception
DRunning
Attempts:
2 left
💡 Hint
Check how multiple upper bounds allow calling methods from both types.
🔧 Debug
advanced
2: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)
}
AProperty 'name' is not defined in Animal, causing compilation error
BDog does not extend Animal properly
CGeneric constraint syntax is incorrect
DNo compilation error, code runs fine
Attempts:
2 left
💡 Hint
Check if the property 'name' exists in the upper bound type Animal.
📝 Syntax
advanced
2:00remaining
Correct syntax for generic function with upper bound
Which option shows the correct syntax for a generic function with an upper bound Animal?
Afun <T : Animal> doSomething(animal: T) { println(animal) }
Bfun <T Animal> doSomething(animal: T) { println(animal) }
Cfun <T> doSomething(animal: T : Animal) { println(animal) }
Dfun <T> doSomething(animal: T) where T Animal { println(animal) }
Attempts:
2 left
💡 Hint
Recall the syntax for declaring upper bounds in Kotlin generics.
🚀 Application
expert
3: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)
}
ACompilation error due to conflicting methods
B
Tesla brand: Model S
Battery full
C
Vehicle brand: Model S
Battery full
DRuntime error due to interface method
Attempts:
2 left
💡 Hint
Check which info() method is called and how multiple upper bounds work.