Recall & Review
beginner
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: <br>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?
✗ 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?
✗ 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?
✗ Incorrect
Type parameters must be separated by commas inside angle brackets.
How do you add constraints to multiple type parameters in Kotlin?
✗ Incorrect
Constraints use a colon ':' after the type parameter name.
What will this code print?<br>val box = Box(10, "hello")<br>println(box.first)<br>println(box.second)
✗ 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.