Challenge - 5 Problems
Swift Number Types 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 mixed number types?
Consider the following Swift code snippet. What will be printed when it runs?
Swift
let a: Int = 5 let b: Double = 3.2 let c = Double(a) + b print(c)
Attempts:
2 left
💡 Hint
Remember that you need to convert Int to Double before adding.
✗ Incorrect
The Int value 5 is converted to Double 5.0, then added to 3.2, resulting in 8.2.
❓ Predict Output
intermediate2:00remaining
What is the value of variable 'result' after this Swift code runs?
Look at this Swift code. What is the value of 'result'?
Swift
let x: Float = 2.5 let y: Double = 4.5 let result = Double(x) * y print(result)
Attempts:
2 left
💡 Hint
Float must be converted to Double before multiplication.
✗ Incorrect
Float 2.5 is converted to Double 2.5, multiplied by 4.5, resulting in 11.25.
❓ Predict Output
advanced2:00remaining
What error does this Swift code produce?
Examine this Swift code snippet. What error will it cause when compiled?
Swift
let a: Int = 10 let b: Float = 3.5 let c = a + b print(c)
Attempts:
2 left
💡 Hint
Swift does not allow adding Int and Float without conversion.
✗ Incorrect
You must convert either Int to Float or Float to Int before adding; otherwise, compiler error occurs.
🧠 Conceptual
advanced2:00remaining
Which Swift type is best for precise decimal calculations?
You want to store currency values in Swift with exact decimal precision. Which type should you use?
Attempts:
2 left
💡 Hint
Floating point types can cause rounding errors.
✗ Incorrect
Decimal type is designed for precise decimal arithmetic, ideal for currency.
❓ Predict Output
expert2:00remaining
How many items are in the resulting array after this Swift code runs?
Analyze this Swift code and determine the number of elements in 'results'.
Swift
let numbers: [Double] = [1.0, 2.5, 3.7, 4.0] let results = numbers.compactMap { num -> Int? in let intValue = Int(num) return num == Double(intValue) ? intValue : nil } print(results.count)
Attempts:
2 left
💡 Hint
compactMap filters out nil values after checking if Double equals Int conversion.
✗ Incorrect
Only 1.0 and 4.0 are whole numbers; 2.5 and 3.7 are filtered out, so count is 2.