0
0
Swiftprogramming~20 mins

Int, Double, Float number types in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Number Types 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 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)
A8.2
B8
CType error at compile time
D5.32
Attempts:
2 left
💡 Hint
Remember that you need to convert Int to Double before adding.
Predict Output
intermediate
2: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)
AType mismatch error
B11.25f
C7.0
D11.25
Attempts:
2 left
💡 Hint
Float must be converted to Double before multiplication.
Predict Output
advanced
2: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)
APrints 13.5
BPrints 13
CType error: Cannot add Int and Float directly
DRuntime error: nil value
Attempts:
2 left
💡 Hint
Swift does not allow adding Int and Float without conversion.
🧠 Conceptual
advanced
2: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?
AFloat
BDecimal
CInt
DDouble
Attempts:
2 left
💡 Hint
Floating point types can cause rounding errors.
Predict Output
expert
2: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)
A2
B3
C1
D4
Attempts:
2 left
💡 Hint
compactMap filters out nil values after checking if Double equals Int conversion.