Bird
0
0

Which of the following Swift statements correctly tests if two class instances x and y are the same object?

easy📝 Syntax Q3 of 15
Swift - Operators and Expressions
Which of the following Swift statements correctly tests if two class instances x and y are the same object?
Aif x == y { print("Same instance") }
Bif x === y { print("Same instance") }
Cif x = y { print("Same instance") }
Dif x !== y { print("Same instance") }
Step-by-Step Solution
Solution:
  1. Step 1: Identify identity operator

    Use === to check if two references point to the same instance.
  2. Step 2: Analyze options

    if x === y { print("Same instance") } uses === correctly. if x == y { print("Same instance") } uses == which checks value equality, not identity. if x = y { print("Same instance") } is an assignment, not a comparison. if x !== y { print("Same instance") } checks inequality, not equality.
  3. Final Answer:

    if x === y { print("Same instance") } -> Option B
  4. Quick Check:

    Use === for identity comparison [OK]
Quick Trick: Use === to check if two references are identical [OK]
Common Mistakes:
  • Using == instead of === for identity
  • Using assignment operator = instead of comparison
  • Confusing !== with ===

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes