Bird
Raised Fist0
Kotlinprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the syntax <T : Number> mean in Kotlin generics?
easy
A. T must be a subtype of the Number class
B. T can be any type including Number
C. T must be a supertype of Number
D. T must be exactly the Number class

Solution

  1. Step 1: Understand the upper bound syntax

    The syntax <T : Number> means T is limited to types that inherit from Number.
  2. Step 2: Interpret subtype relationship

    This means T can be Number or any subclass like Int or Double, but not unrelated types.
  3. Final Answer:

    T must be a subtype of the Number class -> Option A
  4. Quick Check:

    Upper bound <T : Number> = subtype limit [OK]
Hint: Look for ': Type' to mean 'subtype of Type' [OK]
Common Mistakes:
  • Thinking T can be any type
  • Confusing subtype with supertype
  • Assuming exact type match only
2. Which of the following is the correct syntax to declare a generic function with an upper bound on type T to implement the Comparable interface?
easy
A. fun <T> max(a: T, b: T): T where T implements Comparable<T> { ... }
B. fun max(a: T, b: T): T { ... }
C. fun max(a: T, b: T): T { ... }
D. fun max(a: T, b: T): T { ... }

Solution

  1. Step 1: Recall Kotlin's upper bound syntax

    Kotlin uses <T : Interface> to specify T must implement that interface.
  2. Step 2: Identify correct syntax for Comparable

    The correct syntax is fun <T : Comparable<T>> .... Options B and D use Java syntax, and A uses a where clause but with incorrect syntax.
  3. Final Answer:

    fun <T : Comparable<T>> max(a: T, b: T): T { ... } -> Option B
  4. Quick Check:

    Kotlin upper bound uses ':' not 'implements' or 'extends' [OK]
Hint: Use ':' for upper bounds, not 'implements' or 'extends' [OK]
Common Mistakes:
  • Using Java syntax keywords like 'implements' or 'extends'
  • Confusing where clause syntax
  • Missing generic type parameter declaration
3. What will be the output of this Kotlin code?
fun <T : Number> printDouble(value: T) {
    println(value.toDouble() * 2)
}

fun main() {
    printDouble(5)
    printDouble(3.5)
}
medium
A. 5 and 3.5
B. Runtime error due to toDouble() call
C. Compilation error due to type mismatch
D. 10.0 and 7.0

Solution

  1. Step 1: Understand the generic constraint

    The function requires T to be a subtype of Number, so Int and Double are valid.
  2. Step 2: Calculate the output

    For 5 (Int), toDouble() is 5.0, multiplied by 2 is 10.0.
    For 3.5 (Double), toDouble() is 3.5, multiplied by 2 is 7.0.
  3. Final Answer:

    10.0 and 7.0 -> Option D
  4. Quick Check:

    Number constraint allows toDouble(), output matches calculations [OK]
Hint: Remember Number types have toDouble() method [OK]
Common Mistakes:
  • Expecting compilation error for Int argument
  • Confusing printed values with input values
  • Thinking toDouble() causes runtime error
4. Identify the error in this Kotlin code snippet:
fun <T : Comparable> compareValues(a: T, b: T): T {
    return if (a > b) a else b
}
medium
A. No error, code is correct
B. Cannot use '>' operator with Comparable
C. Missing generic type parameter in Comparable
D. Function must return Boolean, not T

Solution

  1. Step 1: Check Comparable usage

    Comparable is a generic interface and requires a type parameter, so it should be Comparable<T>.
  2. Step 2: Understand operator usage

    The '>' operator is not directly supported for Comparable, but Kotlin allows it if Comparable<T> is specified and operator overloading is present.
  3. Final Answer:

    Missing generic type parameter in Comparable -> Option C
  4. Quick Check:

    Comparable must be parameterized as Comparable<T> [OK]
Hint: Always specify type parameter for Comparable like Comparable<T> [OK]
Common Mistakes:
  • Omitting generic parameter in Comparable
  • Assuming '>' works without Comparable<T>
  • Confusing return type requirements
5. Given this Kotlin function:
fun <T : Number> sumList(numbers: List<T>): Double {
    return numbers.sumOf { it.toDouble() }
}

Which of these calls will cause a compilation error?
hard
A. sumList(listOf("1", "2", "3"))
B. sumList(listOf(1.5, 2.5, 3.5))
C. sumList(listOf(1, 2, 3))
D. sumList(listOf(1L, 2L, 3L))

Solution

  1. Step 1: Analyze the function's type constraint

    The function requires a List of elements where each element is a subtype of Number.
  2. Step 2: Check each call's argument type

    Options B, C, and D pass lists of Double, Int, and Long respectively.
    sumList(listOf("1", "2", "3")) passes a list of Strings, which are not subtypes of Number, causing a compilation error.
  3. Final Answer:

    sumList(listOf("1", "2", "3")) -> Option A
  4. Quick Check:

    Only Number subtypes allowed, String list fails [OK]
Hint: Only Number subtypes allowed in sumList argument [OK]
Common Mistakes:
  • Passing String list instead of Number list
  • Assuming any list works
  • Ignoring generic type constraints
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard