0
0
Kotlinprogramming~10 mins

Type constraints with upper bounds 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 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'
A<:
B:
C>:
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' instead of '<:' for upper bounds.
Using '>' or '=' symbols which are incorrect here.
2fill in blank
medium

Complete 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'
A:
B>:
C<:
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' instead of '<:'.
Confusing upper bound syntax with inheritance syntax.
3fill in blank
hard

Fix 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'
A<:
B:
C>:
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' which is incorrect for upper bounds.
Using '=' or '>' symbols.
4fill in blank
hard

Fill 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'
A<:
B:
C>:
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing ':' and '<:' symbols.
Using '>' or '=' which are incorrect.
5fill in blank
hard

Fill 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'
A<:
B:
C>:
D=
Attempts:
3 left
💡 Hint
Common Mistakes
Using ':' or other symbols instead of '<:'.
Inconsistent symbols for different type parameters.