Type inference is when the Swift compiler automatically figures out the type of a variable or constant based on the value you assign to it, so you don't have to write the type explicitly.
Click to reveal answer
beginner
How does Swift infer the type of <code>let number = 10</code>?
Swift sees the value 10 is an integer, so it infers the type of number as Int.
Click to reveal answer
beginner
Can you give an example where type inference helps make code simpler?
Instead of writing <code>let name: String = "Alice"</code>, you can write <code>let name = "Alice"</code>. Swift infers <code>name</code> is a <code>String</code>.
Click to reveal answer
intermediate
What happens if Swift cannot infer the type?
If Swift cannot figure out the type from the value, it will give an error and ask you to specify the type explicitly.
Click to reveal answer
intermediate
Does type inference affect performance at runtime?
No, type inference happens only at compile time. The compiled program runs with explicit types, so there is no performance cost.
Click to reveal answer
What type does Swift infer for let pi = 3.14?
ADouble
BInt
CString
DFloat
✗ Incorrect
Swift infers decimal numbers like 3.14 as Double by default.
Which of these requires explicit type annotation in Swift?
Alet count = 5
Blet message = "Hello"
Clet emptyArray = []
Dlet isActive = true
✗ Incorrect
An empty array has no value to infer type from, so you must specify its type explicitly.
What is the benefit of type inference?
AAutomatically fixes bugs
BSlows down the program
CRemoves the need for variables
DMakes code shorter and easier to read
✗ Incorrect
Type inference helps write less code and keeps it clear without losing type safety.
If you write let value = 42, what type does Swift assign to value?
AString
BInt
CDouble
DBool
✗ Incorrect
42 is an integer, so Swift infers the type Int.
What happens if you try to assign a different type to a variable after type inference?
ASwift throws a compile-time error
BSwift allows it without error
CSwift changes the variable's type automatically
DSwift ignores the new value
✗ Incorrect
Swift enforces type safety, so assigning a different type causes a compile-time error.
Explain what type inference is and why it is useful in Swift programming.
Think about how Swift guesses the type from the value you assign.
You got /4 concepts.
Describe a situation where you must specify the type explicitly even when using type inference.
When there is no value to guess the type from.
You got /4 concepts.
Practice
(1/5)
1. What does type inference mean in Swift?
easy
A. The program runs slower because types are guessed.
B. You must always write the type explicitly.
C. The compiler automatically figures out the variable's type.
D. You cannot use variables without specifying types.
Solution
Step 1: Understand type inference concept
Type inference means the compiler guesses the type of a variable based on the value assigned.
Step 2: Compare options with definition
Only The compiler automatically figures out the variable's type. correctly describes automatic type detection by the compiler.
Final Answer:
The compiler automatically figures out the variable's type. -> Option C
Quick Check:
Type inference = automatic type detection [OK]
Hint: Type inference means compiler guesses type automatically [OK]
Common Mistakes:
Thinking you must always write types explicitly
Believing type inference slows down the program
Confusing type inference with dynamic typing
2. Which of the following Swift variable declarations uses type inference correctly?
easy
A. let number = 10
B. let number: = 10
C. let number Int = 10
D. let number : Int 10
Solution
Step 1: Check syntax for variable declaration with type inference
Type inference allows omitting the type, so let number = 10 is correct.
Step 2: Identify syntax errors in other options
Options B, C, and D have syntax errors: missing type name, wrong order, or missing assignment operator.
Final Answer:
let number = 10 -> Option A
Quick Check:
Correct syntax with inference = let number = 10 [OK]
Hint: Look for correct Swift syntax without explicit type [OK]
Common Mistakes:
Adding colon without type name
Writing type before variable name incorrectly
Omitting assignment operator
3. What is the type of variable value in this Swift code?
let value = 3.14
medium
A. Double
B. Int
C. String
D. Float
Solution
Step 1: Identify the literal type assigned
The number 3.14 is a decimal number, which Swift treats as Double by default.
Step 2: Confirm Swift default for floating-point literals
Swift infers floating-point literals as Double unless specified otherwise.
Final Answer:
Double -> Option A
Quick Check:
3.14 literal = Double type [OK]
Hint: Decimal numbers default to Double in Swift [OK]
Common Mistakes:
Assuming 3.14 is Float by default
Confusing Int with floating-point types
Thinking literals default to String
4. Identify the error in this Swift code snippet:
var name = "Alice"
name = 42
medium
A. No error, type inference allows changing type.
B. Error: Cannot assign Int to a variable inferred as String.
C. Error: Missing semicolon after first line.
D. Error: Variable must be declared with explicit type.
Solution
Step 1: Check initial type inferred for variable
Variable name is assigned a String "Alice", so compiler infers type String.
Step 2: Analyze reassignment with different type
Assigning an Int (42) to a String variable causes a type mismatch error.
Final Answer:
Error: Cannot assign Int to a variable inferred as String. -> Option B
Quick Check:
Type mismatch on reassignment = Error [OK]
Hint: Variable type fixed by first assignment, cannot change later [OK]
Common Mistakes:
Thinking type inference allows changing variable type
Assuming semicolons are required in Swift
Believing explicit type declaration is always needed
5. Given this Swift code, what is the type of result?
let numbers = [1, 2, 3]
let result = numbers.map { $0 * 2 }
hard
A. Array<Any>
B. Int
C. [Double]
D. [Int]
Solution
Step 1: Understand the type of numbers
Array numbers contains Int values, so its type is [Int].
Step 2: Analyze the map operation and type inference
The map multiplies each Int by 2, producing another Int for each element, so the result is an array of Ints.
Step 3: Confirm inferred type of result
Since map returns an array of transformed elements, result is inferred as [Int].
Final Answer:
[Int] -> Option D
Quick Check:
map on [Int] with Int output = [Int] [OK]
Hint: map returns array of same type as transformed elements [OK]
Common Mistakes:
Thinking map returns a single Int
Assuming map changes type to Double automatically
Believing result is Array<Any> without type inference
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.