Bird
Raised Fist0
Kotlinprogramming~20 mins

Comparison operators in Kotlin - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following Kotlin comparison operators checks if two values are equal?
easy
A. <=
B. !=
C. >
D. ==

Solution

  1. Step 1: Understand the meaning of comparison operators

    In Kotlin, the operator == is used to check if two values are equal.
  2. Step 2: Identify the correct operator for equality

    The operator == returns true if both values are equal, otherwise false.
  3. Final Answer:

    == -> Option D
  4. Quick Check:

    Equality operator = == [OK]
Hint: Remember: '==' means equal, '!=' means not equal [OK]
Common Mistakes:
  • Confusing '==' with '=' which is assignment
  • Using '!=' to check equality
  • Mixing up '>' and '<=' operators
2. Which of the following is the correct Kotlin syntax to check if variable a is not equal to variable b?
easy
A. a != b
B. a <> b
C. a =! b
D. a !== b

Solution

  1. Step 1: Recall Kotlin syntax for inequality

    Kotlin uses != to check if two values are not equal.
  2. Step 2: Verify each option

    a != b is correct; a <> b is invalid in Kotlin; a =! b is incorrect syntax; a !== b checks referential inequality, not value inequality.
  3. Final Answer:

    a != b -> Option A
  4. Quick Check:

    Not equal operator = != [OK]
Hint: Use '!=' for not equal, never '<>' in Kotlin [OK]
Common Mistakes:
  • Using '<>' which is not valid in Kotlin
  • Swapping '=' and '!' in 'a =! b'
  • Confusing '!=' with '!==', which checks references
3. What will be the output of the following Kotlin code?
val x = 10
val y = 20
println(x > y)
medium
A. false
B. true
C. 10
D. Compilation error

Solution

  1. Step 1: Evaluate the comparison expression

    The expression x > y compares if 10 is greater than 20, which is false.
  2. Step 2: Understand the println output

    Printing the boolean result of the comparison will output false.
  3. Final Answer:

    false -> Option A
  4. Quick Check:

    10 > 20 = false [OK]
Hint: Compare values carefully; 10 is not greater than 20 [OK]
Common Mistakes:
  • Assuming 10 > 20 is true
  • Confusing output with variable values
  • Expecting a number instead of boolean output
4. Identify the error in this Kotlin code snippet:
val a = 5
val b = 10
if (a = b) {
    println("Equal")
} else {
    println("Not Equal")
}
medium
A. Variables a and b must be mutable
B. Missing semicolon after println
C. Using '=' instead of '==' in if condition
D. Incorrect println syntax

Solution

  1. Step 1: Check the if condition syntax

    The condition uses a = b, which is assignment, not comparison.
  2. Step 2: Correct the operator for comparison

    Use == to compare values, so it should be a == b.
  3. Final Answer:

    Using '=' instead of '==' in if condition -> Option C
  4. Quick Check:

    Comparison needs '==' not '=' [OK]
Hint: Use '==' for comparison, '=' is assignment [OK]
Common Mistakes:
  • Using '=' in if condition instead of '=='
  • Thinking semicolons are required in Kotlin
  • Assuming val variables must be mutable for comparison
5. Given the Kotlin code below, what will be the output?
val a = 7
val b = 7
val c = 10
println((a == b) && (b < c))
hard
A. false
B. true
C. 7
D. Compilation error

Solution

  1. Step 1: Evaluate the first comparison (a == b)

    Since both a and b are 7, a == b is true.
  2. Step 2: Evaluate the second comparison (b < c)

    Since 7 is less than 10, b < c is true.
  3. Step 3: Evaluate the logical AND

    Both conditions are true, so true && true is true.
  4. Final Answer:

    true -> Option B
  5. Quick Check:

    (7 == 7) && (7 < 10) = true [OK]
Hint: Both conditions must be true for && to return true [OK]
Common Mistakes:
  • Confusing && with || operator
  • Misreading comparison results
  • Expecting numeric output instead of boolean
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard