0
0
Swiftprogramming~20 mins

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

Choose your learning style9 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.