0
0
Kotlinprogramming~10 mins

Star projection for unknown types 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 list with star projection.

Kotlin
val list: List<[1]> = listOf("apple", "banana", "cherry")
Drag options to blanks, or click blank then click option'
A*
Bout Any
CAny
D?
Attempts:
3 left
💡 Hint
Common Mistakes
Using '?' instead of '*' for star projection.
Using 'Any' which is a specific type, not a star projection.
2fill in blank
medium

Complete the function parameter type using star projection.

Kotlin
fun printList(list: List<[1]>) {
    for (item in list) {
        println(item)
    }
}
Drag options to blanks, or click blank then click option'
A*
BAny
Cout Any
Din Any
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Any' which restricts the type to Any only.
Using 'out Any' or 'in Any' which are variance annotations, not star projections.
3fill in blank
hard

Fix the error in the generic function using star projection.

Kotlin
fun <T> copy(from: Array<out [1]>, to: Array<[1]>) {
    for (i in from.indices) {
        to[i] = from[i]
    }
}
Drag options to blanks, or click blank then click option'
AAny
Bout Any
CT
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' inside function generic parameters which is invalid.
Using 'Any' which loses type safety.
4fill in blank
hard

Fill both blanks to create a map with star projections for keys and values.

Kotlin
val map: Map<[1], [2]> = mapOf("one" to 1, "two" to 2)
Drag options to blanks, or click blank then click option'
A*
BAny
Cout Any
Din Any
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Any' which is a specific type, not star projection.
Mixing variance annotations like 'out Any' or 'in Any' incorrectly.
5fill in blank
hard

Fill all three blanks to declare a function with star projections and variance.

Kotlin
fun process(list: List<[1]>, producer: Producer<[2]>, consumer: Consumer<[3]>) {
    // function body
}
Drag options to blanks, or click blank then click option'
A*
Bout Any
Cin Any
DAny
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Any' instead of variance annotations for producer and consumer.
Using star projection in variance positions incorrectly.