0
0
Kotlinprogramming~10 mins

Multiple type parameters in Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a generic class with two type parameters.

Kotlin
class Pair<[1], B>(val first: [1], val second: B)
Drag options to blanks, or click blank then click option'
AK
BA
CX
DT
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase letters for type parameters.
Using non-letter characters.
2fill in blank
medium

Complete the function signature to use two generic type parameters.

Kotlin
fun <[1], [2]> createPair(first: [1], second: [2]): Pair<[1], [2]> = Pair(first, second)
Drag options to blanks, or click blank then click option'
AT
BU
CV
DW
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same type parameter twice.
Not declaring generic parameters before the function name.
3fill in blank
hard

Fix the error in the generic class declaration with two type parameters.

Kotlin
class Box<[1], [2]>(val content1: [1], val content2: [2])
Drag options to blanks, or click blank then click option'
AT
BT, U
CT U
DU
Attempts:
3 left
💡 Hint
Common Mistakes
Separating type parameters with spaces or semicolons.
Using only one type parameter when two are needed.
4fill in blank
hard

Fill both blanks to create a generic function that swaps two values of different types.

Kotlin
fun <[1], [2]> swap(pair: Pair<[1], [2]>): Pair<[2], [1]> = Pair(pair.second, pair.first)
Drag options to blanks, or click blank then click option'
AT
BU
CV
DW
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same type parameter for both elements.
Not swapping the type parameters in the return type.
5fill in blank
hard

Fill all three blanks to define a generic class with three type parameters and a method returning a Triple.

Kotlin
class TripleHolder<[1], [2], [3]>(val first: [1], val second: [2], val third: [3]) {
    fun toTriple(): Triple<[1], [2], [3]> = Triple(first, second, third)
}
Drag options to blanks, or click blank then click option'
AA
BB
CC
DD
Attempts:
3 left
💡 Hint
Common Mistakes
Using fewer than three type parameters.
Not matching the type parameters in the method return type.