0
0
Kotlinprogramming~20 mins

Multiple type parameters in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Multiple Type Parameters
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a generic function with two type parameters
What is the output of this Kotlin code?
Kotlin
fun <A, B> pairToString(a: A, b: B): String {
    return "Pair: $a and $b"
}

fun main() {
    println(pairToString(10, "apples"))
}
APair: apples and 10
B10 apples
CPair: 10 and apples
DCompilation error
Attempts:
2 left
💡 Hint
Look at the order of parameters and how they are used in the string.
🧠 Conceptual
intermediate
2:00remaining
Understanding multiple type parameters in Kotlin classes
Which statement correctly describes the following Kotlin class declaration? class Box(val first: A, val second: B)
ABox can hold two values of any types specified when creating an instance.
BBox can only hold values of the same type for both first and second.
CBox requires A and B to be subclasses of the same parent class.
DBox can only hold primitive types like Int and String.
Attempts:
2 left
💡 Hint
Think about what type parameters mean in Kotlin generics.
Predict Output
advanced
2:00remaining
Output of a generic function swapping two values
What is the output of this Kotlin code?
Kotlin
fun <X, Y> swap(pair: Pair<X, Y>): Pair<Y, X> {
    return Pair(pair.second, pair.first)
}

fun main() {
    val original = Pair("hello", 42)
    val swapped = swap(original)
    println(swapped)
}
A(42, hello)
B(hello, 42)
CCompilation error due to type mismatch
D("42", "hello")
Attempts:
2 left
💡 Hint
The function swaps the first and second values in the pair.
🔧 Debug
advanced
2:00remaining
Identify the error in this generic function with multiple type parameters
What error does this Kotlin code produce when compiled? fun combine(a: T, b: U): T { return b } fun main() { println(combine(5, "test")) }
AType inference failed for T and U
BUnresolved reference: combine
CNo error, prints "test"
DType mismatch: inferred type is U but T was expected
Attempts:
2 left
💡 Hint
Look at the return type and the returned value types.
📝 Syntax
expert
2:00remaining
Correct syntax for a Kotlin function with multiple type parameters and constraints
Which option shows the correct syntax for a Kotlin function with two type parameters where T must be a Number and U must be a Comparable of U?
Afun <T : Number, U : Comparable> compareValues(a: T, b: U): Int { return b.compareTo(b) }
Bfun <T : Number, U : Comparable<U>> compareValues(a: T, b: U): Int { return b.compareTo(b) }
Cfun <T Number, U Comparable<U>> compareValues(a: T, b: U): Int { return b.compareTo(b) }
Dfun <T, U> compareValues(a: T : Number, b: U : Comparable<U>): Int { return b.compareTo(b) }
Attempts:
2 left
💡 Hint
Look for correct use of colon and angle brackets for constraints.