Bird
Raised Fist0
Kotlinprogramming~20 mins

Equality (== structural vs === referential) in Kotlin - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Kotlin Equality Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of structural vs referential equality
What is the output of this Kotlin code snippet?
Kotlin
data class Person(val name: String)

fun main() {
    val p1 = Person("Alice")
    val p2 = Person("Alice")
    println(p1 == p2)
    println(p1 === p2)
}
A
false
false
B
false
true
C
true
true
D
true
false
Attempts:
2 left
💡 Hint
Remember that '==' checks structural equality and '===' checks if both variables point to the same object.
❓ Predict Output
intermediate
2:00remaining
Equality with mutable objects
What will this Kotlin code print?
Kotlin
class Box(var value: Int)

fun main() {
    val b1 = Box(10)
    val b2 = Box(10)
    println(b1 == b2)
    println(b1 === b2)
}
A
true
true
B
true
false
C
false
false
D
false
true
Attempts:
2 left
💡 Hint
Default '==' for classes without overriding equals compares references.
🔧 Debug
advanced
2:00remaining
Why does this equality check fail?
Consider this Kotlin code. Why does the equality check print false even though the objects have the same data?
Kotlin
class Point(val x: Int, val y: Int)

fun main() {
    val p1 = Point(1, 2)
    val p2 = Point(1, 2)
    println(p1 == p2)
}
ABecause Point class does not override equals(), so '==' compares references which are different.
BBecause '==' always compares references in Kotlin, regardless of class.
CBecause the data in p1 and p2 are different types.
DBecause the println statement is missing parentheses.
Attempts:
2 left
💡 Hint
Check if the class overrides equals() or is a data class.
🧠 Conceptual
advanced
1:30remaining
Difference between == and === in Kotlin
Which statement correctly describes the difference between '==' and '===' in Kotlin?
A'==' checks structural equality by calling equals(), '===' checks referential equality (same object).
B'==' checks referential equality, '===' checks structural equality.
C'==' and '===' both check structural equality but in different ways.
D'==' and '===' are interchangeable and mean the same.
Attempts:
2 left
💡 Hint
Think about how Kotlin compares objects by content vs by reference.
❓ Predict Output
expert
2:30remaining
Output with nullable types and equality
What is the output of this Kotlin code?
Kotlin
data class User(val id: Int)

fun main() {
    val u1: User? = User(5)
    val u2: User? = null
    println(u1 == u2)
    println(u1 === u2)
    println(u2 == null)
    println(u2 === null)
}
A
false
false
false
false
B
false
false
true
true
C
true
false
true
false
D
true
true
true
true
Attempts:
2 left
💡 Hint
Remember how Kotlin handles nulls with '==' and '==='.

Practice

(1/5)
1. In Kotlin, what does the operator == check when comparing two variables?
easy
A. If the two variables are the exact same object (referential equality)
B. If the two variables have the same content (structural equality)
C. If the two variables are of the same type
D. If the two variables are both null

Solution

  1. Step 1: Understand the meaning of == in Kotlin

    The == operator checks if two variables have the same content, meaning their values are equal.
  2. Step 2: Differentiate from ===

    The === operator checks if two variables point to the exact same object in memory, which is different from ==.
  3. Final Answer:

    If the two variables have the same content (structural equality) -> Option B
  4. Quick Check:

    == means structural equality [OK]
Hint: Remember: == compares values, not object identity [OK]
Common Mistakes:
  • Confusing == with ===
  • Thinking == checks object identity
  • Assuming == checks type equality
2. Which of the following Kotlin expressions correctly checks if two variables refer to the exact same object?
easy
A. a === b
B. a != b
C. a.equals(b)
D. a == b

Solution

  1. Step 1: Identify the operator for referential equality

    In Kotlin, === checks if two variables point to the exact same object in memory.
  2. Step 2: Confirm other options

    == and .equals() check structural equality, and != checks inequality, so they don't check referential equality.
  3. Final Answer:

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

    === means referential equality [OK]
Hint: Use === to check if two variables are the same object [OK]
Common Mistakes:
  • Using == instead of === for object identity
  • Confusing .equals() with ===
  • Assuming != checks referential equality
3. What will be the output of the following Kotlin code?
val a = "hello"
val b = String(charArrayOf('h', 'e', 'l', 'l', 'o'))
println(a == b)
println(a === b)
medium
A. true\nfalse
B. false\nfalse
C. true\ntrue
D. false\ntrue

Solution

  1. Step 1: Evaluate a == b

    Both strings contain the same characters "hello", so a == b is true because it checks structural equality.
  2. Step 2: Evaluate a === b

    They are two different String objects in memory, so a === b is false because it checks referential equality.
  3. Final Answer:

    true\nfalse -> Option A
  4. Quick Check:

    == true, === false [OK]
Hint: == checks content, === checks if same object [OK]
Common Mistakes:
  • Assuming === is true because contents match
  • Confusing == with ===
  • Thinking different objects with same content are ===
4. Consider this Kotlin code snippet:
val x = listOf(1, 2, 3)
val y = listOf(1, 2, 3)
if (x === y) {
    println("Same object")
} else {
    println("Different objects")
}

What is the output and why?
medium
A. Same object
B. Compilation error
C. Different objects
D. Runtime exception

Solution

  1. Step 1: Understand listOf creates new objects

    Each call to listOf(1, 2, 3) creates a new list object, so x and y are different objects.
  2. Step 2: Check referential equality with ===

    Since x and y are different objects, x === y is false, so the else branch runs.
  3. Final Answer:

    Different objects -> Option C
  4. Quick Check:

    === false for different list objects [OK]
Hint: listOf creates new objects; === checks identity [OK]
Common Mistakes:
  • Assuming lists with same content are ===
  • Confusing structural and referential equality
  • Expecting compilation or runtime errors
5. You want to check if two variables a and b in Kotlin either have the same content or are the exact same object. Which code snippet correctly prints "Equal" if either condition is true, and "Not Equal" otherwise?
hard
A.
if (a === b) {
    println("Equal")
} else if (a == b) {
    println("Not Equal")
} else {
    println("Equal")
}
B.
if (a === b && a == b) {
    println("Equal")
} else {
    println("Not Equal")
}
C.
if (a == b) {
    println("Not Equal")
} else if (a === b) {
    println("Equal")
} else {
    println("Not Equal")
}
D.
if (a == b || a === b) {
    println("Equal")
} else {
    println("Not Equal")
}

Solution

  1. Step 1: Understand the condition needed

    We want to print "Equal" if either a == b (same content) OR a === b (same object) is true.
  2. Step 2: Check each option's logic

    if (a == b || a === b) {
        println("Equal")
    } else {
        println("Not Equal")
    }
    uses logical OR (||) correctly.
    if (a === b && a == b) {
        println("Equal")
    } else {
        println("Not Equal")
    }
    requires both to be true (AND), which is too strict.
    if (a == b) {
        println("Not Equal")
    } else if (a === b) {
        println("Equal")
    } else {
        println("Not Equal")
    }
    incorrectly prints "Not Equal" when a == b is true.
    if (a === b) {
        println("Equal")
    } else if (a == b) {
        println("Not Equal")
    } else {
        println("Equal")
    }
    prints "Not Equal" incorrectly when a == b is true but a === b is false.
  3. Final Answer:

    if (a == b || a === b) -> Option D
  4. Quick Check:

    Use || to combine == and === checks [OK]
Hint: Use || to check either structural or referential equality [OK]
Common Mistakes:
  • Using && instead of ||
  • Confusing order of conditions
  • Printing wrong messages for conditions
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