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
intermediate2: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) }
Attempts:
2 left
💡 Hint
Think about how logical AND works with comparison operators.
✗ Incorrect
The expression 'a < b && b < c' checks if a is less than b AND b is less than c. Both are true, so the result is true.
❓ Predict Output
intermediate2: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) }
Attempts:
2 left
💡 Hint
Remember the difference between '==' and '===' in Kotlin.
✗ Incorrect
'==' checks structural equality (content), so x == y is true. '===' checks referential equality (same object). Since both are identical string literals, they refer to the same instance in the constant pool, so true.
❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
Kotlin's type system prevents direct comparisons involving nullable types.
✗ Incorrect
Kotlin does not define comparison operators like '<' for nullable types (Int?) because they do not implement Comparable. This results in a compilation error.
❓ Predict Output
advanced2: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) }
Attempts:
2 left
💡 Hint
Check how the compareTo function affects the '<' operator.
✗ Incorrect
The '<' operator uses compareTo. Since 3 - 5 is negative, box1 < box2 is true.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Recall how IEEE floating-point standard treats NaN in comparisons.
✗ Incorrect
According to IEEE rules, any comparison with NaN using '<' or '>' returns false, no exception is thrown.