What does it mean that type conversion is always explicit in Swift?
It means you must clearly tell Swift when you want to convert one type to another. Swift won't do it automatically for you.
Click to reveal answer
beginner
How do you convert an Int to a Double in Swift?
You write Double(yourIntValue). For example, Double(5) converts the integer 5 to a double 5.0.
Click to reveal answer
intermediate
Why does Swift require explicit type conversion?
To avoid mistakes and bugs by making sure you know exactly when and how data changes type.
Click to reveal answer
beginner
What happens if you try to add an Int and a Double without conversion in Swift?
Swift will give an error because it does not automatically convert types. You must convert one type explicitly before adding.
Click to reveal answer
intermediate
Show an example of explicit type conversion from String to Int in Swift.
You use Int("123") which tries to convert the string "123" to the integer 123. It returns an optional Int because the conversion might fail.
Click to reveal answer
In Swift, how do you convert an Int value 10 to a Double?
ADouble(10)
B10 as Double
Cconvert(10, to: Double)
D10.toDouble()
✗ Incorrect
You use Double(10) to explicitly convert the integer 10 to a double 10.0.
What happens if you try to add an Int and a Double directly in Swift without conversion?
AYou get a compile-time error.
BSwift automatically converts Int to Double.
CIt works fine.
DThe result is always an Int.
✗ Incorrect
Swift requires explicit conversion, so adding Int and Double without converting causes a compile-time error.
Which of these is a correct way to convert a String "42" to an Int in Swift?
A"42" as Int
BInt("42")
Cconvert("42", to: Int)
D"42" toInt()
✗ Incorrect
Int("42") attempts to convert the string to an Int and returns an optional Int.
Why does Swift require explicit type conversion?
ATo allow automatic type guessing.
BTo make code run faster.
CBecause Swift is an old language.
DTo avoid unexpected bugs by making conversions clear.
✗ Incorrect
Explicit conversion helps avoid bugs by making sure you know when types change.
Which statement is true about type conversion in Swift?
ASwift automatically converts types when needed.
BType conversion is implicit for numeric types only.
CType conversion must always be explicit.
DYou cannot convert types in Swift.
✗ Incorrect
Swift requires you to explicitly convert types; it does not do it automatically.
Explain why Swift requires explicit type conversion and give an example converting Int to Double.
Think about safety and clarity in code.
You got /2 concepts.
Describe how to convert a String to an Int in Swift and what happens if the string cannot be converted.
Remember the conversion returns an optional because it might fail.
You got /3 concepts.
Practice
(1/5)
1. In Swift, why must type conversion be explicit? let x = 5 and let y = 3.2 are different types. What happens if you try to add x + y without conversion?
easy
A. Swift automatically converts x to a Double and adds them.
B. It causes a compile-time error because Swift does not convert types automatically.
C. The program runs but the result is always an integer.
D. Swift converts y to an Int silently.
Solution
Step 1: Understand Swift's type safety
Swift requires explicit type conversion to avoid unexpected behavior.
Step 2: Check adding Int and Double without conversion
Trying to add Int and Double directly causes a compile-time error.
Final Answer:
It causes a compile-time error because Swift does not convert types automatically. -> Option B
Quick Check:
Int + Double without conversion causes compile error [OK]
Hint: Remember: Swift never converts types automatically [OK]
Common Mistakes:
Assuming Swift converts Int to Double automatically
Thinking the result is always an Int
Believing silent conversion happens
2. Which of the following is the correct way to convert an Int num = 10 to a Double in Swift?
easy
A. let d = convert(num, to: Double)
B. let d = num as Double
C. let d = Double(num)
D. let d = num.toDouble()
Solution
Step 1: Recall Swift's explicit conversion syntax
Swift uses the target type name as a function to convert values.
Step 2: Identify correct syntax for Int to Double
The correct syntax is Double(num). Other options are invalid in Swift.
Final Answer:
let d = Double(num) -> Option C
Quick Check:
Target type as function: Double(num) [OK]
Hint: Use target type name with parentheses for conversion [OK]
Common Mistakes:
Using 'as' for numeric conversion
Calling non-existent methods like toDouble()
Using custom convert functions not in Swift
3. What is the output of this Swift code?
let a: Int = 7
let b: Double = 3.5
let c = Double(a) + b
print(c)
medium
A. 10.5
B. 10
C. 7.0
D. Error: Cannot add Int and Double
Solution
Step 1: Convert Int to Double explicitly
a is Int 7, converted to Double 7.0 using Double(a).
Step 2: Add converted Double and b
Adding 7.0 + 3.5 results in 10.5.
Final Answer:
10.5 -> Option A
Quick Check:
7.0 + 3.5 = 10.5 [OK]
Hint: Convert Int to Double before adding [OK]
Common Mistakes:
Forgetting to convert Int to Double
Expecting integer addition result
Assuming implicit conversion
4. Identify the error in this Swift code snippet:
let str = "123"
let num = Int(str)
let result = num + 10
print(result)
medium
A. Missing parentheses in Int conversion
B. String cannot be converted to Int
C. No error, code runs fine
D. Cannot add Int? and Int without unwrapping
Solution
Step 1: Understand Int initializer returns optional
Int(str) returns Int? because conversion might fail.
Step 2: Adding optional Int and Int causes error
You cannot add Int? and Int directly without unwrapping.
Final Answer:
Cannot add Int? and Int without unwrapping -> Option D
Quick Check:
Int(str) returns Int?, must unwrap before adding [OK]
Hint: Unwrap optionals before arithmetic operations [OK]
Common Mistakes:
Ignoring optional unwrapping
Assuming Int(str) returns Int directly
Trying to add optional and non-optional
5. You have an array of strings representing numbers: let arr = ["1", "2", "three", "4"]. You want to convert them to Ints and sum only the valid numbers. Which Swift code correctly does this?
hard
A. let sum = arr.compactMap { Int($0) }.reduce(0, +)
B. let sum = arr.map { Int($0) }.reduce(0, +)
C. let sum = arr.reduce(0) { $0 + Int($1)! }
D. let sum = arr.reduce(0) { $0 + Int($1) }
Solution
Step 1: Use compactMap to convert and filter valid Ints
compactMap converts strings to Ints and removes nil values safely.
Step 2: Use reduce to sum the valid Ints
reduce(0, +) sums all Ints in the array.
Final Answer:
let sum = arr.compactMap { Int($0) }.reduce(0, +) -> Option A
Quick Check:
compactMap filters nils, then sum [OK]
Hint: Use compactMap to safely convert and filter nils [OK]
Common Mistakes:
Using map which keeps optionals causing errors
Force unwrapping without checking validity
Adding optionals directly without unwrapping
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.