Bird
0
0

You want to check if two data class instances have the same content and also if they are the exact same object. Which Kotlin code correctly does both checks?

hard📝 Application Q8 of 15
Kotlin - Operators and Expressions
You want to check if two data class instances have the same content and also if they are the exact same object. Which Kotlin code correctly does both checks?
Aif (obj1 == obj2) println("Same content") if (obj1 === obj2) println("Same object")
Bif (obj1 === obj2) println("Same content") if (obj1 == obj2) println("Same object")
Cif (obj1.equals(obj2)) println("Same object") if (obj1 === obj2) println("Same content")
Dif (obj1.equals(obj2)) println("Same object") if (obj1.equals(obj2)) println("Same content")
Step-by-Step Solution
Solution:
  1. Step 1: Understand data class equality

    Data classes override equals() so == checks structural equality (content).
  2. Step 2: Use === for referential equality

    === checks if both variables point to the same object.
  3. Step 3: Match code to requirements

    if (obj1 == obj2) println("Same content") if (obj1 === obj2) println("Same object") correctly uses == for content and === for object identity.
  4. Final Answer:

    Option A correctly checks both content and reference equality -> Option A
  5. Quick Check:

    Content = ==, Object = === [OK]
Quick Trick: Use == for content, === for object identity [OK]
Common Mistakes:
MISTAKES
  • Swapping == and === meanings
  • Using equals() incorrectly for reference check
  • Confusing content and reference equality

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Kotlin Quizzes