0
0
Kotlinprogramming~10 mins

Generic class declaration 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 named Box.

Kotlin
class Box<[1]> {
    var value: T? = null
}
Drag options to blanks, or click blank then click option'
AT
BInt
CString
DAny
Attempts:
3 left
💡 Hint
Common Mistakes
Using a concrete type like Int instead of a generic type parameter.
Forgetting the angle brackets <> around the generic type.
2fill in blank
medium

Complete the code to create an instance of the generic class Box with type String.

Kotlin
val box: Box<[1]> = Box()
Drag options to blanks, or click blank then click option'
AInt
BString
CDouble
DBoolean
Attempts:
3 left
💡 Hint
Common Mistakes
Using the generic type parameter T instead of a concrete type.
Omitting the type argument when creating the instance.
3fill in blank
hard

Fix the error in the generic class declaration by completing the blank.

Kotlin
class Container<[1]> {
    fun getValue(): T? = null
}
Drag options to blanks, or click blank then click option'
AData
Bvalue
CT
DType
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name instead of a type parameter.
Mismatch between the declared type parameter and its usage.
4fill in blank
hard

Fill both blanks to declare a generic class Pair with two type parameters.

Kotlin
class Pair<[1], [2]> {
    var first: A? = null
    var second: B? = null
}
Drag options to blanks, or click blank then click option'
AA
BT
CB
DK
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same letter for both type parameters.
Mismatch between type parameter names and property types.
5fill in blank
hard

Fill all three blanks to declare a generic class Triple with three type parameters and properties.

Kotlin
class Triple<[1], [2], [3]> {
    var first: X? = null
    var second: Y? = null
    var third: Z? = null
}
Drag options to blanks, or click blank then click option'
AX
BY
CZ
DT
Attempts:
3 left
💡 Hint
Common Mistakes
Using fewer or duplicate type parameters.
Mismatch between type parameters and property types.