0
0
Kotlinprogramming~20 mins

Comparison operators in Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Comparison Operators in Kotlin
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of chained comparison operators
What is the output of this Kotlin code snippet?
Kotlin
fun main() {
    val a = 5
    val b = 10
    val c = 15
    println(a < b && b < c)
}
Afalse
Btrue
CCompilation error
DRuntime exception
Attempts:
2 left
💡 Hint
Think about how logical AND works with comparison operators.
Predict Output
intermediate
2:00remaining
Result of equality and identity operators
What will be printed by this Kotlin code?
Kotlin
fun main() {
    val x = "hello"
    val y = "hello"
    println(x == y)
    println(x === y)
}
Atrue\nfalse
Bfalse\nfalse
Cfalse\ntrue
Dtrue\ntrue
Attempts:
2 left
💡 Hint
Remember the difference between '==' and '===' in Kotlin.
Predict Output
advanced
2:00remaining
Output of comparison with nullable types
What is the output of this Kotlin code?
Kotlin
fun main() {
    val a: Int? = null
    val b: Int? = 5
    println(a < b)
}
ACompilation error
Btrue
Cfalse
DRuntime exception
Attempts:
2 left
💡 Hint
Kotlin's type system prevents direct comparisons involving nullable types.
Predict Output
advanced
2:00remaining
Output of custom compareTo implementation
Given this Kotlin class and code, what is printed?
Kotlin
class Box(val size: Int) : Comparable<Box> {
    override fun compareTo(other: Box): Int = this.size - other.size
}

fun main() {
    val box1 = Box(3)
    val box2 = Box(5)
    println(box1 < box2)
}
ACompilation error
Bfalse
Ctrue
DRuntime exception
Attempts:
2 left
💡 Hint
Check how the compareTo function affects the '<' operator.
🧠 Conceptual
expert
2:00remaining
Behavior of comparison operators with floating-point NaN
In Kotlin, what is the result of comparing a Double.NaN with any number using '<' or '>' operators?
ABoth '<' and '>' return false when comparing NaN with any number
B'<' returns true and '>' returns false when comparing NaN with any number
C'<' returns false and '>' returns true when comparing NaN with any number
DBoth '<' and '>' throw a runtime exception when comparing NaN with any number
Attempts:
2 left
💡 Hint
Recall how IEEE floating-point standard treats NaN in comparisons.