0
0
Swiftprogramming~20 mins

Identity comparison (===) 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 identity comparison with class instances?
Consider the following Swift code. What will be printed when it runs?
Swift
class Person {
    var name: String
    init(name: String) {
        self.name = name
    }
}

let p1 = Person(name: "Alice")
let p2 = Person(name: "Alice")
let p3 = p1

print(p1 === p2)
print(p1 === p3)
Afalse\ntrue
Btrue\nfalse
Cfalse\nfalse
Dtrue\ntrue
Attempts:
2 left
💡 Hint
Remember that === checks if two references point to the exact same object in memory.
🧠 Conceptual
intermediate
1:30remaining
Which statement about === in Swift is true?
Choose the correct statement about the identity operator === in Swift.
A<code>===</code> compares the values of two structs.
B<code>===</code> checks if two strings have the same characters.
C<code>===</code> can be used to compare two integers.
D<code>===</code> compares if two class instances are the same object.
Attempts:
2 left
💡 Hint
Think about what kinds of types === works with in Swift.
🔧 Debug
advanced
2:00remaining
Why does this code cause a compile error?
Examine the code below. Why does the line with if a === b cause a compile error?
Swift
struct Point {
    var x: Int
    var y: Int
}

let a = Point(x: 1, y: 2)
let b = Point(x: 1, y: 2)

if a === b {
    print("Same")
} else {
    print("Different")
}
ABecause <code>Point</code> does not implement the Equatable protocol.
BBecause <code>a</code> and <code>b</code> are constants, not variables.
CBecause <code>===</code> cannot be used with structs, only with class instances.
DBecause <code>===</code> requires both operands to be optional types.
Attempts:
2 left
💡 Hint
Recall the difference between value types and reference types in Swift.
Predict Output
advanced
2:30remaining
What is the output when comparing optional class instances with ===?
What will this Swift code print?
Swift
class Box {
    let value: Int
    init(_ value: Int) { self.value = value }
}

let box1: Box? = Box(10)
let box2: Box? = Box(10)
let box3: Box? = box1
let box4: Box? = nil

print(box1 === box2)
print(box1 === box3)
print(box1 === box4)
Afalse\ntrue\nfalse
Btrue\ntrue\nfalse
Cfalse\nfalse\ntrue
Dtrue\nfalse\nfalse
Attempts:
2 left
💡 Hint
Remember that === works with optionals by unwrapping them and comparing references.
🚀 Application
expert
3:00remaining
How many unique instances are in this array?
Given the following Swift code, how many unique class instances does the array arr contain?
Swift
class Node {}

let n1 = Node()
let n2 = Node()
let n3 = n1
let n4 = n2
let n5 = Node()

let arr = [n1, n2, n3, n4, n5]
A5
B3
C4
D2
Attempts:
2 left
💡 Hint
Use identity comparison to find which variables point to the same instance.