0
0
Kotlinprogramming~20 mins

Out variance (covariance) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Covariance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Kotlin code using out variance?
Consider the following Kotlin code that uses out variance in a generic interface. What will be printed when running the main function?
Kotlin
interface Producer<out T> {
    fun produce(): T
}

class StringProducer : Producer<String> {
    override fun produce(): String = "Hello"
}

fun printProduced(producer: Producer<Any>) {
    println(producer.produce())
}

fun main() {
    val stringProducer: Producer<String> = StringProducer()
    printProduced(stringProducer)
}
AHello
BPrints null
CRuntime exception
DCompilation error due to type mismatch
Attempts:
2 left
💡 Hint
Remember that out variance allows a subtype to be used where a supertype is expected.
🧠 Conceptual
intermediate
2:00remaining
Why does Kotlin allow Producer to be assigned to Producer when out is used?
Choose the best explanation for why Kotlin permits this assignment when out variance is declared.
ABecause <code>out</code> means the generic type is only produced (returned), so it is safe to treat a <code>Producer<String></code> as a <code>Producer<Any></code>.
BBecause Kotlin ignores generic types at runtime, so all assignments are allowed.
CBecause <code>out</code> means the generic type can be both produced and consumed, allowing any assignment.
DBecause <code>Producer<String></code> and <code>Producer<Any></code> are unrelated types and Kotlin allows unsafe casts automatically.
Attempts:
2 left
💡 Hint
Think about what out means for how the generic type is used inside the interface.
🔧 Debug
advanced
2:00remaining
What error does this Kotlin code produce?
Examine the code below. What error will the Kotlin compiler report?
Kotlin
interface Consumer<in T> {
    fun consume(item: T)
}

fun acceptConsumer(consumer: Consumer<Any>) {}

fun main() {
    val stringConsumer: Consumer<String> = object : Consumer<String> {
        override fun consume(item: String) {
            println(item)
        }
    }
    acceptConsumer(stringConsumer)
}
ARuntime exception due to type cast
BNo error, code compiles and runs printing the item
CType mismatch: inferred type is Consumer<String> but Consumer<Any> was expected
DSyntax error: missing override keyword
Attempts:
2 left
💡 Hint
Consider how in variance affects assignment compatibility.
Predict Output
advanced
2:00remaining
What is the output of this Kotlin code with mixed variance?
Given the following Kotlin code, what will be printed when main runs?
Kotlin
interface Box<out T> {
    fun get(): T
}

class IntBox(private val value: Int) : Box<Int> {
    override fun get(): Int = value
}

fun printBox(box: Box<Number>) {
    println(box.get())
}

fun main() {
    val intBox: Box<Int> = IntBox(42)
    printBox(intBox)
}
APrints null
B42
CCompilation error due to type mismatch
DRuntime exception
Attempts:
2 left
💡 Hint
Remember that out variance allows a subtype to be used where a supertype is expected.
🧠 Conceptual
expert
2:00remaining
Why can't you use out variance on a generic type parameter if the type is consumed as a function parameter?
Select the best reason why Kotlin forbids declaring a generic type parameter as out if it is used as a function parameter type inside the interface or class.
ABecause function parameters are always covariant regardless of variance annotations.
BBecause Kotlin requires all generic parameters to be invariant if used in functions.
CBecause <code>out</code> variance only applies to classes, not interfaces.
DBecause <code>out</code> means the type can only be produced (returned), so using it as a function parameter (consumed) breaks type safety.
Attempts:
2 left
💡 Hint
Think about what out means and how function parameters use types.