Recall & Review
beginner
What is type inference in Swift?
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?✗ Incorrect
Swift infers decimal numbers like 3.14 as Double by default.
Which of these requires explicit type annotation in Swift?
✗ 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?
✗ 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?✗ 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?
✗ 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.