Bird
0
0

Consider the code:

medium📝 Predict Output Q5 of 15
Swift - Operators and Expressions
Consider the code:
class Cat {}

let c1 = Cat()
let c2 = c1
let c3 = Cat()

print(c1 !== c2)
print(c1 !== c3)

What is the output?
Atrue\nfalse
Bfalse\ntrue
Ctrue\ntrue
Dfalse\nfalse
Step-by-Step Solution
Solution:
  1. Step 1: Analyze identity inequality checks

    c1 and c2 refer to the same instance, so c1 !== c2 is false. c1 and c3 are different instances, so c1 !== c3 is true.
  2. Step 2: Determine print outputs

    First print outputs false, second print outputs true.
  3. Final Answer:

    false\ntrue -> Option B
  4. Quick Check:

    c1 !== c2 = false, c1 !== c3 = true [OK]
Quick Trick: !== returns true only if references differ [OK]
Common Mistakes:
  • Confusing !== with != for value inequality
  • Assuming c1 !== c2 is true
  • Mixing up identity and value comparisons

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes