Challenge - 5 Problems
Swift Comparison Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Think about how logical AND works with comparison operators.
✗ Incorrect
The expression checks if a is less than b AND b is less than c. Both are true, so the result is true.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Check if x and y are equal or not.
✗ Incorrect
x == y is true because both are 7; x != y is false because they are equal.
❓ Predict Output
advanced2: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)
Attempts:
2 left
💡 Hint
Remember operator precedence: && before ||.
✗ Incorrect
q > r && r == 9 is false && true = false; p < q || false = true || false = true.
❓ Predict Output
advanced2:00remaining
Output of comparing optional values
What will this Swift code print?
Swift
let a: Int? = nil let b: Int? = 5 print(a == b)
Attempts:
2 left
💡 Hint
Comparing nil to a value returns false.
✗ Incorrect
Optional values can be compared; nil is not equal to 5, so false is printed.
❓ Predict Output
expert3: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)
Attempts:
2 left
💡 Hint
The > operator uses the < operator logic reversed.
✗ Incorrect
box1.volume (10) is not greater than box2.volume (20), so false is printed.