Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a generic function with an upper bound on type parameter T.
Kotlin
fun <T [1] Number> printNumber(value: T) {
println(value)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' instead of '<:' for upper bounds.
Using '>' or '=' symbols which are incorrect here.
✗ Incorrect
In Kotlin, to specify an upper bound for a generic type parameter, use the '<:' symbol.
2fill in blank
mediumComplete the code to declare a class with a generic type parameter T that must be a subtype of Comparable<T>.
Kotlin
class Box<T [1] Comparable<T>> { fun compareTo(other: T): Int { return 0 } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' instead of '<:'.
Confusing upper bound syntax with inheritance syntax.
✗ Incorrect
The '<:' symbol is used to specify that T must be a subtype of Comparable.
3fill in blank
hardFix the error in the generic function declaration to correctly specify the upper bound for T.
Kotlin
fun <T [1] Any> printValue(value: T) {
println(value)
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' which is incorrect for upper bounds.
Using '=' or '>' symbols.
✗ Incorrect
The correct syntax for upper bounds in Kotlin generics is '<:'.
4fill in blank
hardFill both blanks to declare a generic function where T is a subtype of Number and R is a subtype of Comparable<R>.
Kotlin
fun <T [1] Number, R [2] Comparable<R>> process(value: T, other: R) { println(value) println(other) }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing ':' and '<:' symbols.
Using '>' or '=' which are incorrect.
✗ Incorrect
Both T and R use '<:' to specify their upper bounds in Kotlin generics.
5fill in blank
hardFill all three blanks to declare a generic class with T, R, and S having upper bounds Number, Comparable<R>, and Any respectively.
Kotlin
class Container<T [1] Number, R [2] Comparable<R>, S [3] Any> { fun display(t: T, r: R, s: S) { println(t) println(r) println(s) } }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' or other symbols instead of '<:'.
Inconsistent symbols for different type parameters.
✗ Incorrect
All type parameters use '<:' to specify their upper bounds in Kotlin generics.