Challenge - 5 Problems
Swift Type Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of explicit type conversion in Swift
What is the output of this Swift code snippet?
Swift
let intVal: Int = 10 let doubleVal: Double = 3.5 let result = Double(intVal) + doubleVal print(result)
Attempts:
2 left
💡 Hint
Remember that Swift requires explicit conversion between Int and Double types.
✗ Incorrect
The integer 10 is explicitly converted to Double before addition, so 10.0 + 3.5 equals 13.5.
❓ Predict Output
intermediate2:00remaining
Result of forced type conversion with optional
What will be printed by this Swift code?
Swift
let str = "123" let num = Int(str)! print(num + 1)
Attempts:
2 left
💡 Hint
The string contains only digits, so conversion to Int succeeds.
✗ Incorrect
Int(str) converts the string "123" to integer 123. Forced unwrapping (!) succeeds, so 123 + 1 equals 124.
❓ Predict Output
advanced2:00remaining
What error occurs with implicit conversion?
What error does this Swift code produce?
Swift
let a: Int = 5 let b: Double = 2.0 let c = a + b print(c)
Attempts:
2 left
💡 Hint
Swift does not allow implicit conversion between Int and Double in arithmetic operations.
✗ Incorrect
Swift requires explicit conversion; adding Int and Double without conversion causes a compile-time type error.
❓ Predict Output
advanced2:00remaining
Output of converting Double to Int explicitly
What is the output of this Swift code?
Swift
let pi: Double = 3.99 let intPi = Int(pi) print(intPi)
Attempts:
2 left
💡 Hint
Converting Double to Int truncates the decimal part.
✗ Incorrect
Int(pi) truncates the decimal part, so 3.99 becomes 3.
🧠 Conceptual
expert2:00remaining
Why is explicit type conversion required in Swift?
Which of the following best explains why Swift requires explicit type conversion between numeric types?
Attempts:
2 left
💡 Hint
Think about safety and clarity in code.
✗ Incorrect
Explicit conversions make the programmer aware of possible data loss or changes, preventing subtle bugs.