Recall & Review
beginner
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?
✗ 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?
✗ 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?
✗ Incorrect
Int("42") attempts to convert the string to an Int and returns an optional Int.
Why does Swift require explicit type conversion?
✗ Incorrect
Explicit conversion helps avoid bugs by making sure you know when types change.
Which statement is true about type conversion 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.