Challenge - 5 Problems
Covariance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Remember that
out variance allows a subtype to be used where a supertype is expected.✗ Incorrect
The
out keyword makes the generic type covariant, so Producer can be used as Producer. The produce method returns a String, which is a subtype of Any, so printing it outputs "Hello".🧠 Conceptual
intermediate2: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.Attempts:
2 left
💡 Hint
Think about what
out means for how the generic type is used inside the interface.✗ Incorrect
out means the generic type is only used as an output (return type). This makes the type covariant, so a Producer can safely be used where a Producer is expected, because String is a subtype of Any.🔧 Debug
advanced2: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) }
Attempts:
2 left
💡 Hint
Consider how
in variance affects assignment compatibility.✗ Incorrect
The
Consumer interface is contravariant in T (marked with in). Because of contravariance, since String is a subtype of Any, Consumer<String> is not a subtype of Consumer<Any>. Therefore, passing a Consumer<String> to a function expecting Consumer<Any> causes a type mismatch error.❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
Remember that
out variance allows a subtype to be used where a supertype is expected.✗ Incorrect
Because
Box is declared with out T, Box can be used as Box. The get method returns 42, which is printed.🧠 Conceptual
expert2: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.Attempts:
2 left
💡 Hint
Think about what
out means and how function parameters use types.✗ Incorrect
The
out variance means the generic type is only used as output (return type). If you use it as a function parameter (input), it violates type safety because you could pass a wrong subtype. Kotlin forbids this to keep type safety.