0
0
Kotlinprogramming~20 mins

Star projection for unknown types in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Star Projection Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
}
ARuntime exception due to star projection
B1\ntwo\n3.0\nnull
C1\ntwo\n3.0
DCompilation error due to unknown type
Attempts:
2 left
💡 Hint
Star projection allows reading elements as type Any?, so printing works fine.
🧠 Conceptual
intermediate
1:30remaining
Understanding star projection type safety
Which statement about Kotlin star projection (e.g., List<*>) is correct?
AYou can only read elements as type Any? from a List<*>.
BYou can add elements of any type to a List<*> safely.
CList<*> is equivalent to List<Any>.
DList<*> allows casting elements to their original type without checks.
Attempts:
2 left
💡 Hint
Think about what operations are safe when the exact generic type is unknown.
🔧 Debug
advanced
2: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?
ABecause MutableList<*> does not allow adding elements due to unknown generic type.
BBecause element is of type Any, which is incompatible with MutableList<*>.
CBecause add() requires a nullable type but element is non-nullable.
DBecause MutableList<*> is immutable and does not have add() method.
Attempts:
2 left
💡 Hint
Think about what star projection means for mutable collections.
📝 Syntax
advanced
1: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?
Afun process(items: List<?>) { /*...*/ }
Bfun process(items: List<Any>) { /*...*/ }
Cfun process(items: List<*>) { /*...*/ }
Dfun process(items: List<out Any>) { /*...*/ }
Attempts:
2 left
💡 Hint
Kotlin uses star (*) for unknown generic types, not question marks.
🚀 Application
expert
2: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?
A
fun printElements(list: List&lt;Any?&gt;) {
    for (item in list) {
        println(item)
    }
}
B
fun printElements(list: List&lt;Any&gt;) {
    for (item in list) {
        println(item)
    }
}
C
fun printElements(list: List&lt;out Any&gt;) {
    for (item in list) {
        println(item)
    }
}
D
fun printElements(list: List&lt;*&gt;) {
    for (item in list) {
        println(item)
    }
}
Attempts:
2 left
💡 Hint
Star projection allows reading elements as Any? regardless of the unknown type.