Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '?' instead of '*' for star projection.
Using 'Any' which is a specific type, not a star projection.
✗ Incorrect
The star projection is represented by '*' in Kotlin generics to indicate an unknown type.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
Using '*' as star projection allows the function to accept a list of any type.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '*' inside function generic parameters which is invalid.
Using 'Any' which loses type safety.
✗ Incorrect
The generic type parameter 'T' should be used consistently instead of star projection in this function.
4fill in blank
hardFill 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'
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.
✗ Incorrect
Star projection '*' is used for both key and value types to indicate unknown types.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Any' instead of variance annotations for producer and consumer.
Using star projection in variance positions incorrectly.
✗ Incorrect
Star projection '*' is used for the list, 'out Any' for producer (covariant), and 'in Any' for consumer (contravariant).