0
0
Swiftprogramming~20 mins

Comparison operators in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of chained comparison operators
What is the output of this Swift code snippet?
Swift
let a = 5
let b = 10
let c = 15
print(a < b && b < c)
Atrue
Bfalse
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Think about how logical AND works with comparison operators.
Predict Output
intermediate
2:00remaining
Result of equality and inequality operators
What will be printed by this Swift code?
Swift
let x = 7
let y = 7
print(x == y, x != y)
Atrue false
Bfalse true
Ctrue true
Dfalse false
Attempts:
2 left
💡 Hint
Check if x and y are equal or not.
Predict Output
advanced
2:00remaining
Output of mixed comparison and logical operators
What is the output of this Swift code?
Swift
let p = 3
let q = 6
let r = 9
print(p < q || q > r && r == 9)
ARuntime error
Bfalse
CCompilation error
Dtrue
Attempts:
2 left
💡 Hint
Remember operator precedence: && before ||.
Predict Output
advanced
2:00remaining
Output of comparing optional values
What will this Swift code print?
Swift
let a: Int? = nil
let b: Int? = 5
print(a == b)
ACompilation error
Bfalse
Ctrue
DRuntime error
Attempts:
2 left
💡 Hint
Comparing nil to a value returns false.
Predict Output
expert
3:00remaining
Output of custom Comparable struct comparison
Given this Swift code, what is the output?
Swift
struct Box: Comparable {
    let volume: Int
    static func < (lhs: Box, rhs: Box) -> Bool {
        lhs.volume < rhs.volume
    }
}
let box1 = Box(volume: 10)
let box2 = Box(volume: 20)
print(box1 > box2)
Atrue
BRuntime error
Cfalse
DCompilation error
Attempts:
2 left
💡 Hint
The > operator uses the < operator logic reversed.