0
0
Kotlinprogramming~20 mins

In variance (contravariance) in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Contravariance 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 contravariance?
Consider the following Kotlin code snippet. What will be printed when it runs?
Kotlin
interface Consumer<in T> {
    fun consume(item: T)
}

class StringConsumer : Consumer<String> {
    override fun consume(item: String) {
        println("Consumed: $item")
    }
}

fun feedConsumer(consumer: Consumer<Any>) {
    consumer.consume(123)
}

fun main() {
    val stringConsumer: Consumer<String> = StringConsumer()
    feedConsumer(stringConsumer)
}
AType mismatch error at compile time
BConsumed: 123
CConsumed: StringConsumer@<hashcode>
DRuntime exception: ClassCastException
Attempts:
2 left
💡 Hint
Think about how contravariance affects type compatibility in Kotlin generics.
🧠 Conceptual
intermediate
1:30remaining
Which statement correctly describes contravariance in Kotlin?
Choose the correct statement about contravariance in Kotlin generics.
AContravariance allows a generic type to accept subtypes of its type parameter.
BContravariance means the generic type parameter is covariant and can only produce values.
CContravariance means the generic type parameter is invariant and cannot vary.
DContravariance allows a generic type to accept supertypes of its type parameter.
Attempts:
2 left
💡 Hint
Think about the direction of type substitution allowed by 'in' keyword.
🔧 Debug
advanced
2:00remaining
Why does this Kotlin code cause a compilation error?
Examine the Kotlin code below. Why does it fail to compile?
Kotlin
interface Producer<out T> {
    fun produce(): T
}

fun useProducer(producer: Producer<String>) {
    val anyProducer: Producer<Any> = producer
    println(anyProducer.produce())
}

fun main() {
    val stringProducer: Producer<String> = object : Producer<String> {
        override fun produce() = "Hello"
    }
    useProducer(stringProducer)
}
ABecause Producer is covariant, assigning Producer<String> to Producer<Any> is invalid.
BBecause Producer is contravariant, assigning Producer<String> to Producer<Any> is invalid.
CBecause Producer is covariant, assigning Producer<String> to Producer<Any> is valid and compiles.
DBecause Producer is contravariant, assigning Producer<String> to Producer<Any> is valid and compiles.
Attempts:
2 left
💡 Hint
Recall the meaning of 'out' keyword in Kotlin generics.
📝 Syntax
advanced
1:30remaining
Which Kotlin declaration correctly defines a contravariant generic interface?
Select the correct Kotlin interface declaration that uses contravariance.
Ainterface Consumer<out T> { fun consume(item: T) }
Binterface Consumer<in T> { fun consume(item: T) }
Cinterface Consumer<T> { fun consume(item: T) }
Dinterface Consumer<in T> { fun produce(): T }
Attempts:
2 left
💡 Hint
Contravariant types use the 'in' keyword and can only consume values of type T.
🚀 Application
expert
2:30remaining
How to safely assign a contravariant consumer in Kotlin?
Given the interface Consumer<in T>, which assignment is valid and safe in Kotlin?
Kotlin
interface Consumer<in T> {
    fun consume(item: T)
}

class AnyConsumer : Consumer<Any> {
    override fun consume(item: Any) {
        println("Consumed: $item")
    }
}

fun main() {
    val anyConsumer = AnyConsumer()
    // Which assignment below is valid?
    val stringConsumer: Consumer<String> = ???
}
AanyConsumer
BConsumer<Any>()
CConsumer<String>()
Dnull
Attempts:
2 left
💡 Hint
Remember contravariance allows assigning Consumer to Consumer.