What does it mean to have multiple type parameters in Kotlin?
It means a generic class or function can work with more than one type, each represented by a separate type parameter like <T, U>.
Click to reveal answer
beginner
How do you declare a Kotlin function with two type parameters?
You write the function with <T, U> before the function name, for example: fun <T, U> pairToString(pair: Pair<T, U>): String { return "${pair.first} and ${pair.second}" }
Click to reveal answer
beginner
What is the benefit of using multiple type parameters?
It lets you write flexible code that can handle different types together, like pairs or maps, without losing type safety.
Click to reveal answer
intermediate
Example: What does this Kotlin code do?<br>class Box<T, U>(val first: T, val second: U)
It defines a generic class Box that holds two values: one of type T and one of type U. You can create Box<Int, String> or Box<Double, Boolean>.
Click to reveal answer
intermediate
Can multiple type parameters have constraints in Kotlin? How?
Yes. You can restrict each type parameter with 'where' clauses or inline constraints, like <T : Number, U : Comparable<U>> to require T to be a Number and U to be comparable.
Click to reveal answer
How do you declare a Kotlin class with two type parameters?
Aclass MyClass<T; U>
Bclass MyClass<T U>
Cclass MyClass<T & U>
Dclass MyClass<T, U>
✗ Incorrect
In Kotlin, multiple type parameters are separated by commas inside angle brackets: <T, U>.
What is the purpose of multiple type parameters in Kotlin generics?
ATo allow a generic type to work with multiple types at once
BTo restrict a generic type to only one type
CTo make the code run faster
DTo avoid using generics
✗ Incorrect
Multiple type parameters let you create generic classes or functions that handle more than one type safely.
Which of these is a valid Kotlin function declaration with two type parameters?
Afun <T U> combine(a: T, b: U): String
Bfun <T, U> combine(a: T, b: U): String
Cfun <T; U> combine(a: T, b: U): String
Dfun <T & U> combine(a: T, b: U): String
✗ Incorrect
Type parameters must be separated by commas inside angle brackets.
How do you add constraints to multiple type parameters in Kotlin?
Afun <T where Number, U where Comparable> doSomething()
Bfun <T Number, U Comparable> doSomething()
Cfun <T : Number, U : Comparable<U>> doSomething()
Dfun <T & Number, U & Comparable> doSomething()
✗ Incorrect
Constraints use a colon ':' after the type parameter name.
What will this code print? val box = Box(10, "hello") println(box.first) println(box.second)
A10<br>hello
Bhello<br>10
CError
Dnull<br>null
✗ Incorrect
box.first holds 10 (Int) and box.second holds "hello" (String), so printing them outputs 10 and hello.
Explain how to declare and use a Kotlin generic class with two type parameters.
Think about a class that holds two different types of values.
You got /3 concepts.
Describe why multiple type parameters are useful in Kotlin programming.
Consider how pairs or maps use two types.
You got /4 concepts.
Practice
(1/5)
1. What does it mean when a Kotlin function has <T, U> before its name?
easy
A. The function is limited to only one type parameter named T or U.
B. The function can only accept types T or U but not both at the same time.
C. The function uses two different type parameters, T and U, to work with multiple types.
D. The function automatically converts types T and U to String.
Solution
Step 1: Understand type parameters in Kotlin
Type parameters like <T, U> allow a function to accept multiple types, making it flexible.
Step 2: Interpret the meaning of <T, U>
Having <T, U> means the function can work with two different types, T and U, independently.
Final Answer:
The function uses two different type parameters, T and U, to work with multiple types. -> Option C
Quick Check:
Multiple type parameters = flexible function types [OK]
Hint: Multiple type parameters mean multiple flexible types [OK]
Common Mistakes:
Thinking it limits to only one type at a time
Assuming automatic type conversion happens
Confusing type parameters with actual types
2. Which of the following is the correct syntax to declare a Kotlin function with two type parameters named T and U?
easy
A. fun myFunction(param1: T, param2: U) {}
B. fun myFunction(param1: T, param2: U) {}
C. fun myFunction(param1: T, param2: U) {}
D. fun myFunction(param1: T, param2: U) {}
Solution
Step 1: Recall Kotlin generic function syntax
Type parameters are declared immediately after the fun keyword inside angle brackets, separated by commas.
Step 2: Check each option's syntax
fun myFunction(param1: T, param2: U) {} correctly uses <T, U> after fun and before the function name, matching Kotlin syntax.
Final Answer:
fun <T, U> myFunction(param1: T, param2: U) {} -> Option A
Quick Check:
Type parameters go after fun with commas [OK]
Hint: Type parameters come right after fun, separated by commas [OK]
Common Mistakes:
Missing comma between type parameters
Placing type parameters after function name
Putting type parameters after parameter list
3. What is the output of this Kotlin code?
fun <T, U> pairToString(pair: Pair<T, U>): String {
return "${pair.first} and ${pair.second}"
}
fun main() {
val result = pairToString(Pair(10, "cats"))
println(result)
}
medium
A. Compilation error
B. Pair(10, cats)
C. cats and 10
D. 10 and cats
Solution
Step 1: Understand the function behavior
The function takes a Pair with types T and U, then returns a string combining first and second values.
Step 2: Trace the main function call
pairToString is called with Pair(10, "cats"), so pair.first = 10 and pair.second = "cats".
Final Answer:
10 and cats -> Option D
Quick Check:
pair.first + " and " + pair.second = "10 and cats" [OK]
Hint: Read pair.first and pair.second values carefully [OK]
Common Mistakes:
Swapping first and second values
Expecting Pair's default toString output
Thinking it causes a compilation error
4. Identify the error in this Kotlin function declaration:
fun <T, U> combine(a: T, b: U): T {
return b
}
medium
A. Return type T does not match returned value b of type U.
B. Missing comma between T and U in type parameters.
C. Function must return type U instead of T.
D. No error, the function is correct.
Solution
Step 1: Check function return type and return statement
The function declares return type T but returns b which is of type U.
Step 2: Understand type mismatch error
Returning U where T is expected causes a type mismatch compilation error.
Final Answer:
Return type T does not match returned value b of type U. -> Option A
Quick Check:
Return type must match returned value type [OK]
Hint: Return type must match the returned value type [OK]
Common Mistakes:
Ignoring type mismatch between T and U
Assuming Kotlin auto-converts types
Missing comma in type parameters (not the case here)
5. You want to create a Kotlin generic class Box that can hold two different types of items, T and U. Which of the following class declarations correctly uses multiple type parameters and allows storing and retrieving both items?
hard
A. class Box<T>(val first: T, val second: T) {
fun getFirst(): T = first
fun getSecond(): T = second
}
B. class Box(val first: T, val second: U) {
fun getFirst(): T = first
fun getSecond(): U = second
}
C. class Box {
val first: T
val second: U
}
D. class Box(val first: T, val second: U) {
fun getFirst(): T = first
fun getSecond(): U = second
}
Solution
Step 1: Check correct multiple type parameter declaration
class Box(val first: T, val second: U) {
fun getFirst(): T = first
fun getSecond(): U = second
} declares class Box with <T, U> and constructor parameters of types T and U, which is correct.
Step 2: Verify methods to retrieve both items
class Box(val first: T, val second: U) {
fun getFirst(): T = first
fun getSecond(): U = second
} provides getFirst() returning T and getSecond() returning U, matching stored types.
Final Answer:
class Box<T, U>(val first: T, val second: U) { fun getFirst(): T = first fun getSecond(): U = second } -> Option B
Quick Check:
Multiple type parameters with matching properties and methods [OK]
Hint: Use and matching properties for both types [OK]
Common Mistakes:
Using only one type parameter for both items
Not declaring type parameters in class header
Using undefined types without generics
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.