Bird
Raised Fist0
Swiftprogramming~20 mins

Identity operators (=== and !==) in Swift - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Swift Identity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this Swift code using identity operator?
Consider the following Swift code. What will be printed to the console?
Swift
class Person {}

let personA = Person()
let personB = personA
let personC = Person()

if personA === personB {
    print("Same instance")
} else {
    print("Different instances")
}

if personA === personC {
    print("Same instance")
} else {
    print("Different instances")
}
ASame instance\nDifferent instances
BDifferent instances\nDifferent instances
CSame instance\nSame instance
DDifferent instances\nSame instance
Attempts:
2 left
💡 Hint
Remember that === checks if two variables point to the exact same object in memory.
❓ Predict Output
intermediate
1:30remaining
What does this code print when using !== operator?
Look at this Swift code. What will be the output?
Swift
class Box {}

let box1 = Box()
let box2 = Box()

if box1 !== box2 {
    print("Boxes are not the same instance")
} else {
    print("Boxes are the same instance")
}
ABoxes are the same instance
BBoxes are not the same instance
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
!== means the two references do not point to the same object.
🧠 Conceptual
advanced
1:00remaining
Which statement about === and !== in Swift is true?
Choose the correct statement about the identity operators === and !== in Swift.
AThey check if two variables refer to the exact same instance in memory.
BThey compare the values of two variables for equality.
CThey can be used to compare structs and enums for identity.
DThey are used to compare two variables for type equality.
Attempts:
2 left
💡 Hint
Think about what identity means in terms of objects and memory.
❓ Predict Output
advanced
2:30remaining
What is the output of this Swift code with optional references and identity check?
Analyze this Swift code and determine what it prints.
Swift
class Animal {}

let animal1: Animal? = Animal()
let animal2: Animal? = animal1
let animal3: Animal? = nil

if animal1 === animal2 {
    print("animal1 and animal2 are identical")
} else {
    print("animal1 and animal2 are not identical")
}

if animal1 !== animal3 {
    print("animal1 and animal3 are not identical")
} else {
    print("animal1 and animal3 are identical")
}
Aanimal1 and animal2 are identical\nanimal1 and animal3 are identical
Banimal1 and animal2 are not identical\nanimal1 and animal3 are identical
Canimal1 and animal2 are identical\nanimal1 and animal3 are not identical
Danimal1 and animal2 are not identical\nanimal1 and animal3 are not identical
Attempts:
2 left
💡 Hint
Optional references can be compared with identity operators; nil is a special case.
🧠 Conceptual
expert
1:30remaining
Why can't identity operators === and !== be used with Swift value types like structs?
Select the best explanation for why === and !== cannot be applied to structs or enums in Swift.
ABecause identity operators are only for comparing primitive types like Int and String.
BBecause structs and enums are stored in the heap, making identity checks meaningless.
CBecause Swift does not allow any comparison operators on structs or enums.
DBecause structs and enums do not have identity; they are copied when assigned, so no single instance exists.
Attempts:
2 left
💡 Hint
Think about how value types behave differently from reference types.

Practice

(1/5)
1. What does the identity operator === check in Swift?
easy
A. If two variables are of the same type
B. If two variables have the same value
C. If two variables refer to the exact same class instance
D. If two variables are both nil

Solution

  1. Step 1: Understand the purpose of ===

    The === operator checks if two references point to the same object in memory.
  2. Step 2: Differentiate from value equality

    Unlike ==, which checks value equality, === checks identity (same instance).
  3. Final Answer:

    If two variables refer to the exact same class instance -> Option C
  4. Quick Check:

    === checks identity [OK]
Hint: Remember: === means same object, not just same value [OK]
Common Mistakes:
  • Confusing === with == for value equality
  • Using === with structs or value types
  • Thinking === checks type equality
2. Which of the following is the correct syntax to check if two class instances a and b are NOT the same object in Swift?
easy
A. if a != b
B. if a !==== b
C. if a !=== b
D. if a !== b

Solution

  1. Step 1: Identify the correct identity operator for inequality

    The operator to check if two references are NOT the same object is !==.
  2. Step 2: Verify syntax correctness

    Options C and D have invalid operators. if a != b uses != which checks value inequality, not identity.
  3. Final Answer:

    if a !== b -> Option D
  4. Quick Check:

    Use !== for identity inequality [OK]
Hint: Use !== to check different objects, not != [OK]
Common Mistakes:
  • Using != instead of !== for identity check
  • Typing invalid operators like !=== or !====
  • Confusing identity operators with value operators
3. Consider the following Swift code:
class Person {}

let p1 = Person()
let p2 = p1
let p3 = Person()

print(p1 === p2)
print(p1 === p3)

What will be the output?
medium
A. true\nfalse
B. false\ntrue
C. true\ntrue
D. false\nfalse

Solution

  1. Step 1: Analyze the references

    p2 is assigned to p1, so both point to the same object. p3 is a new instance.
  2. Step 2: Evaluate identity comparisons

    p1 === p2 is true because they are the same instance. p1 === p3 is false because they are different instances.
  3. Final Answer:

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

    Same instance = true, different instance = false [OK]
Hint: === true if same object, false if different [OK]
Common Mistakes:
  • Assuming p1 and p3 are same because same class
  • Confusing value equality with identity
  • Forgetting p2 points to p1's instance
4. The following Swift code produces an error. What is the problem?
struct Point {
  var x: Int
  var y: Int
}

let p1 = Point(x: 1, y: 2)
let p2 = Point(x: 1, y: 2)

if p1 === p2 {
  print("Same point")
}
medium
A. p1 and p2 are not initialized
B. Cannot use === with structs because they are value types
C. Missing parentheses in if condition
D. === operator requires optional types

Solution

  1. Step 1: Identify the types used

    Point is a struct, which is a value type in Swift.
  2. Step 2: Understand identity operator restrictions

    Identity operators === and !== only work with class instances (reference types), not value types like structs.
  3. Final Answer:

    Cannot use === with structs because they are value types -> Option B
  4. Quick Check:

    === only for class instances [OK]
Hint: Use === only with classes, not structs or enums [OK]
Common Mistakes:
  • Trying to use === with value types
  • Assuming structs have identity
  • Ignoring compiler error message
5. You have a class Car and an array of Car instances. You want to find if a specific Car instance myCar is already in the array by identity (same object), not by value. Which Swift code correctly checks this?
hard
A. if cars.contains(where: { $0 === myCar }) { print("Found") }
B. if cars.contains(myCar) { print("Found") }
C. if cars.firstIndex(of: myCar) != nil { print("Found") }
D. if cars.filter({ $0 == myCar }).count > 0 { print("Found") }

Solution

  1. Step 1: Understand identity check in collections

    To check if an instance exists by identity, use === inside a closure with contains(where:).
  2. Step 2: Analyze options

    if cars.contains(where: { $0 === myCar }) { print("Found") } uses === correctly. Options B and C use == or of: which check value equality or require Equatable conformance. if cars.filter({ $0 == myCar }).count > 0 { print("Found") } uses == which is value equality.
  3. Final Answer:

    if cars.contains(where: { $0 === myCar }) { print("Found") } -> Option A
  4. Quick Check:

    Use contains(where:) with === for identity [OK]
Hint: Use contains(where:) with === to check object identity [OK]
Common Mistakes:
  • Using contains(myCar) which checks value equality
  • Assuming == checks identity
  • Not using closure for identity check
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