Consider the following Swift code snippet. What will be printed?
let number: Int = 5 let result = number * 2 print(result)
Look at the type of number and the operation performed.
The variable number is explicitly declared as an Int. Multiplying it by 2 results in 10, which is printed.
value here?Given the code below, what is the type of value?
let value: Double = 3.14 print(type(of: value))
Check the explicit type annotation used when declaring value.
The variable value is explicitly declared as Double, so its type is Double.
Examine the code below. Why does it fail to compile?
let text: Int = "Hello" print(text)
Look at the type annotation and the assigned value.
The variable text is declared as Int but assigned a String. This causes a type mismatch error.
What will this code print?
let optionalNumber: Int? = 10 if let number: Int = optionalNumber { print(number * 3) } else { print("No value") }
Check how optional binding with explicit type annotation works.
The optional optionalNumber contains 10, so number is assigned 10 and multiplied by 3, printing 30.
Choose the best reason why explicit type annotation is important in Swift programming.
Think about how Swift uses types to prevent bugs.
Explicit type annotation helps the compiler detect type mismatches early and makes the code clearer for humans reading it.