Challenge - 5 Problems
Master of Multiple Type Parameters
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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")) }
Attempts:
2 left
💡 Hint
Look at the order of parameters and how they are used in the string.
✗ Incorrect
The function takes two parameters of any types A and B, then returns a string showing them in the order they were passed. So it prints "Pair: 10 and apples".
🧠 Conceptual
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about what type parameters mean in Kotlin generics.
✗ Incorrect
The class Box has two type parameters A and B, so it can hold two values of any types independently specified when creating an instance.
❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
The function swaps the first and second values in the pair.
✗ Incorrect
The swap function returns a new Pair with the second and first values swapped, so the output is (42, hello).
🔧 Debug
advanced2: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"))
}
Attempts:
2 left
💡 Hint
Look at the return type and the returned value types.
✗ Incorrect
The function declares it returns type T but tries to return b of type U, causing a type mismatch error.
📝 Syntax
expert2: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?
Attempts:
2 left
💡 Hint
Look for correct use of colon and angle brackets for constraints.
✗ Incorrect
Option B correctly uses colon to specify constraints on type parameters inside angle brackets.