Challenge - 5 Problems
Swift Overflow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Swift code with overflow?
Consider the following Swift code using overflow addition. What will be printed?
Swift
var maxUInt8: UInt8 = 255 maxUInt8 = maxUInt8 &+ 1 print(maxUInt8)
Attempts:
2 left
💡 Hint
Think about what happens when an 8-bit unsigned integer goes beyond its max value.
✗ Incorrect
UInt8 can hold values from 0 to 255. Using &+ adds with overflow, so 255 + 1 wraps around to 0.
❓ Predict Output
intermediate2:00remaining
What is the result of this subtraction with overflow?
What will this Swift code print when subtracting with overflow?
Swift
var minInt8: Int8 = -128 minInt8 = minInt8 &- 1 print(minInt8)
Attempts:
2 left
💡 Hint
Remember Int8 ranges from -128 to 127 and &- allows wrapping.
✗ Incorrect
Int8 minimum is -128. Subtracting 1 with &- wraps around to 127.
🔧 Debug
advanced2:00remaining
Why does this Swift code cause a runtime error?
This code tries to add two Int values. Why does it crash?
Swift
let a: Int = Int.max let b: Int = 1 let c = a + b print(c)
Attempts:
2 left
💡 Hint
Check what happens when adding 1 to the maximum Int value without overflow operators.
✗ Incorrect
Swift's default Int addition traps on overflow, causing a runtime error when exceeding Int.max.
🧠 Conceptual
advanced2:00remaining
Which operator safely adds two UInt8 values allowing overflow?
Which Swift operator adds two UInt8 values and allows overflow without crashing?
Attempts:
2 left
💡 Hint
Look for the operator with an ampersand that allows overflow.
✗ Incorrect
The &+ operator performs addition with overflow, wrapping around on overflow instead of crashing.
❓ Predict Output
expert3:00remaining
What is the output of this mixed overflow and normal addition code?
Analyze this Swift code and determine what it prints.
Swift
var x: UInt8 = 250 let y: UInt8 = 10 let z = x &+ y let w = x + y print(z) print(w)
Attempts:
2 left
💡 Hint
Consider which addition allows overflow and which does not.
✗ Incorrect
x &+ y wraps around: 250 + 10 = 260, which overflows UInt8 and wraps to 4.
Normal + causes runtime error on overflow.