Challenge - 5 Problems
Star Projection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of star projection with unknown generic type
What is the output of this Kotlin code using star projection on a generic list?
Kotlin
fun printList(list: List<*>) { for (item in list) { println(item) } } fun main() { val mixedList: List<Any?> = listOf(1, "two", 3.0, null) printList(mixedList) }
Attempts:
2 left
💡 Hint
Star projection allows reading elements as type Any?, so printing works fine.
✗ Incorrect
The star projection List<*> means the list can hold elements of unknown type, but you can safely read elements as Any?. The code prints all elements including null.
🧠 Conceptual
intermediate1:30remaining
Understanding star projection type safety
Which statement about Kotlin star projection (e.g., List<*>) is correct?
Attempts:
2 left
💡 Hint
Think about what operations are safe when the exact generic type is unknown.
✗ Incorrect
Star projection means the generic type is unknown, so you cannot add elements safely, but you can read elements as Any?.
🔧 Debug
advanced2:00remaining
Why does this code cause a compilation error?
Consider this Kotlin code snippet:
fun addElement(list: MutableList<*>, element: Any) {
list.add(element)
}
Why does this code fail to compile?
Attempts:
2 left
💡 Hint
Think about what star projection means for mutable collections.
✗ Incorrect
MutableList<*> means the element type is unknown, so adding any element is unsafe and disallowed by the compiler.
📝 Syntax
advanced1:30remaining
Correct syntax for star projection in Kotlin
Which of the following is the correct way to declare a function parameter using star projection for a generic type?
Attempts:
2 left
💡 Hint
Kotlin uses star (*) for unknown generic types, not question marks.
✗ Incorrect
Kotlin uses star projection syntax List<*> to represent unknown generic types. List> is Java syntax and invalid in Kotlin.
🚀 Application
expert2:30remaining
Using star projection to safely print elements of unknown generic type
You have a function that accepts a List with an unknown generic type. You want to print all elements safely without causing type errors. Which code snippet correctly uses star projection to achieve this?
Attempts:
2 left
💡 Hint
Star projection allows reading elements as Any? regardless of the unknown type.
✗ Incorrect
List<*> means the generic type is unknown but elements can be read as Any?, so printing them is safe.