Bird
0
0

Identify the error in the following Swift code:

medium📝 Debug Q6 of 15
Swift - Operators and Expressions
Identify the error in the following Swift code:
class Bird {}

let b1 = Bird()
let b2 = Bird()

if b1 == b2 {
    print("Same bird")
}
AUsing assignment '=' instead of comparison
BUsing '==' instead of '===' for identity
CMissing parentheses in if condition
DUsing invalid operator '===' instead of '==='
Step-by-Step Solution
Solution:
  1. Step 1: Check operator for identity

    The code uses ==, which compares values, not identity. Use === for same instance.
  2. Step 2: Eliminate other options

    = is assignment but not used here; no invalid operator or missing parens.
  3. Final Answer:

    Using '==' instead of '===' for identity -> Option B
  4. Quick Check:

    Identity: === not == [OK]
Quick Trick: Use === for identity comparison, not == [OK]
Common Mistakes:
  • Typing === instead of ===
  • Confusing assignment '=' with comparison
  • Using == for identity check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes