Explicit type annotation means telling Swift exactly what type a variable or constant should be, by writing the type after the name with a colon. For example, <code>let age: Int = 30</code> means <strong>age</strong> is an integer.
Click to reveal answer
beginner
Why use explicit type annotation in Swift?
You use explicit type annotation to make your code clearer, help the compiler catch mistakes early, and sometimes to fix ambiguous types when Swift can't guess the type on its own.
Click to reveal answer
beginner
How do you write explicit type annotation for a variable named name that holds a string?
You write: var name: String. This tells Swift that name will always hold text data.
Click to reveal answer
intermediate
What happens if you assign a value of the wrong type to a variable with explicit type annotation?
Swift will give a compile-time error because the value does not match the declared type. For example, if var age: Int and you try age = "thirty", it will not work.
Click to reveal answer
beginner
Can you give an example of explicit type annotation with a constant?
Yes! For example: <code>let pi: Double = 3.14159</code> means <strong>pi</strong> is a constant of type Double (decimal number).
Click to reveal answer
What does this Swift code do? var score: Int = 100
ADeclares a variable named score that holds a string value
BDeclares a constant named score with value 100
CDeclares a variable named score that holds an integer value 100
DDeclares a variable named score without a type
✗ Incorrect
The code declares a variable score with explicit type Int and assigns it the value 100.
Why might you use explicit type annotation in Swift?
ATo allow variables to change type later
BTo make the program run faster
CTo avoid writing variable names
DTo make the code clearer and avoid type guessing errors
✗ Incorrect
Explicit type annotation helps the compiler and readers understand exactly what type a variable or constant should have.
What error will Swift give if you write var count: Int = "five"?
ARuntime error when the program runs
BType mismatch error because "five" is a string, not an Int
CSyntax error because of missing semicolon
DNo error, Swift converts string to Int automatically
✗ Incorrect
Swift enforces type safety and will not allow assigning a string to an Int variable.
Which of these is a correct explicit type annotation for a Boolean variable named isOpen?
Avar isOpen: Bool = true
Bvar isOpen: Boolean = true
Cvar isOpen = true
Dvar isOpen: int = true
✗ Incorrect
In Swift, the Boolean type is called Bool, so var isOpen: Bool = true is correct.
What does the colon (:) mean in let name: String?
AIt separates the variable name from its type
BIt assigns a value to the variable
CIt declares a function
DIt comments the code
✗ Incorrect
The colon is used to specify the type of a variable or constant after its name.
Explain what explicit type annotation is and why it is useful in Swift.
Think about how you tell Swift exactly what kind of data a variable holds.
You got /4 concepts.
Write an example of a Swift variable with explicit type annotation and explain each part.
Try something like <code>var age: Int = 25</code> and explain it.
You got /6 concepts.
Practice
(1/5)
1. What does explicit type annotation in Swift do?
easy
A. It removes the need to declare variables.
B. It automatically converts types during runtime.
C. It tells the exact data type of a variable or constant.
D. It makes the program run faster by skipping type checks.
Solution
Step 1: Understand the purpose of explicit type annotation
Explicit type annotation is used to specify the exact data type of a variable or constant in Swift.
Step 2: Compare with other options
The other options describe behaviors unrelated to explicit type annotation, such as automatic conversion, removing the need to declare variables, or performance improvements.
Final Answer:
It tells the exact data type of a variable or constant. -> Option C
Quick Check:
Explicit type annotation = exact data type [OK]
Hint: Explicit type annotation means writing the type after the variable name [OK]
Common Mistakes:
Thinking it changes runtime behavior
Confusing with type inference
Believing it speeds up code execution
2. Which of the following is the correct syntax for explicit type annotation in Swift?
easy
A. var age: Int = 25
B. var age = Int: 25
C. var age Int = 25
D. var age = 25: Int
Solution
Step 1: Recall Swift syntax for explicit type annotation
The correct syntax is to write the variable name, then a colon, then the type, then an equals sign and the value.
Step 2: Check each option
var age: Int = 25 matches this syntax exactly. The other options have misplaced colons or missing punctuation.
Final Answer:
var age: Int = 25 -> Option A
Quick Check:
Variable name : Type = value [OK]
Hint: Remember: variableName: Type = value [OK]
Common Mistakes:
Putting colon after the value
Missing colon between name and type
Using equal sign before type
3. What will be the output of this Swift code?
let pi: Double = 3
print(pi)
medium
A. Error: Cannot assign Int to Double
B. 3
C. 3.00
D. 3.0
Solution
Step 1: Analyze the type annotation and assigned value
The variable pi is declared as a Double but assigned the integer 3. Swift converts the integer 3 to 3.0 automatically.
Step 2: Understand print output for Double
Printing a Double with value 3 prints as 3.0 by default.
Final Answer:
3.0 -> Option D
Quick Check:
Double 3 prints as 3.0 [OK]
Hint: Assigning Int to Double converts to decimal automatically [OK]
Common Mistakes:
Expecting a compile error
Thinking print shows integer 3
Confusing Double formatting
4. Identify the error in this Swift code:
var name: String = 123
print(name)
medium
A. Type mismatch: cannot assign Int to String
B. Variable name is a reserved keyword
C. Missing explicit type annotation
D. No error, prints 123
Solution
Step 1: Check the declared type and assigned value
The variable name is declared as String but assigned an integer 123, which is a type mismatch.
Step 2: Understand Swift's type safety
Swift does not allow assigning an Int to a String variable, causing a compile-time error.
Final Answer:
Type mismatch: cannot assign Int to String -> Option A
Quick Check:
Type mismatch error = Type mismatch: cannot assign Int to String [OK]
Hint: Check if assigned value matches declared type exactly [OK]
Common Mistakes:
Assuming implicit conversion from Int to String
Ignoring type mismatch errors
Thinking variable names are reserved keywords
5. You want to store a list of ages as integers but accidentally write:
var ages: [String] = [20, 25, 30]
What is the best fix using explicit type annotation?
hard
A. Change to var ages = ["20", "25", "30"]
B. Change to var ages: [Int] = [20, 25, 30]
C. Remove type annotation and write var ages = [20, 25, 30]
D. Change to var ages: Int = [20, 25, 30]
Solution
Step 1: Identify the mismatch between type and values
The variable ages is declared as an array of Strings but assigned integer values, causing a type mismatch.
Step 2: Fix by matching type annotation to actual data
Changing the type annotation to [Int] matches the integer values in the array.
Final Answer:
var ages: [Int] = [20, 25, 30] -> Option B
Quick Check:
Array type matches element types [OK]
Hint: Match array type annotation to the element types [OK]
Common Mistakes:
Changing values to strings unnecessarily
Removing type annotation without understanding
Using single Int type for array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.