0
0
Swiftprogramming~20 mins

Type conversion is always explicit in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Type Conversion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A13.5
B13
CType error at compile time
D10.0
Attempts:
2 left
💡 Hint
Remember that Swift requires explicit conversion between Int and Double types.
Predict Output
intermediate
2: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)
A124
B1231
CType error at compile time
DRuntime error: nil unwrapping
Attempts:
2 left
💡 Hint
The string contains only digits, so conversion to Int succeeds.
Predict Output
advanced
2: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)
A7.0
BType error: binary operator '+' cannot be applied to operands of type 'Int' and 'Double'
C5 + 2 = 7
DRuntime error: cannot add Int and Double
Attempts:
2 left
💡 Hint
Swift does not allow implicit conversion between Int and Double in arithmetic operations.
Predict Output
advanced
2: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)
A4
BType error at compile time
C3.99
D3
Attempts:
2 left
💡 Hint
Converting Double to Int truncates the decimal part.
🧠 Conceptual
expert
2:00remaining
Why is explicit type conversion required in Swift?
Which of the following best explains why Swift requires explicit type conversion between numeric types?
ATo improve runtime performance by avoiding automatic conversions
BBecause Swift does not support numeric operations between different types
CTo prevent unexpected data loss or bugs by making conversions clear and intentional
DBecause implicit conversions are handled automatically by the compiler
Attempts:
2 left
💡 Hint
Think about safety and clarity in code.