0
0
iOS Swiftmobile~5 mins

Variables (let, var) and type inference in iOS Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the difference between let and var in Swift?

let declares a constant, which means its value cannot change after it is set. var declares a variable, which means its value can change.

Click to reveal answer
beginner
What does type inference mean in Swift?

Type inference means Swift automatically figures out the type of a variable or constant based on the value you assign to it, so you don't always have to write the type explicitly.

Click to reveal answer
beginner
How do you declare a variable named score with an initial value of 10 using type inference?

You write: var score = 10. Swift infers that score is an Int because 10 is an integer.

Click to reveal answer
beginner
Can you change the value of a constant declared with let after it is set?

No, once a constant is set with let, its value cannot be changed. Trying to do so causes a compile-time error.

Click to reveal answer
intermediate
Why is it good to use let instead of var when possible?

Using let helps keep your code safe and predictable because constants cannot change unexpectedly. It also helps the compiler optimize your code.

Click to reveal answer
Which keyword declares a variable whose value can change in Swift?
Alet
Bvar
Cconst
Dstatic
What type will Swift infer for let name = "Alice"?
AString
BInt
CDouble
DBool
What happens if you try to change a let constant after setting it?
AThe value changes successfully
BThe app crashes at runtime
CYou get a compile-time error
DThe value resets to default
Which is a correct way to declare a variable with type inference?
Avar age: Int = 25
Bvar age = 25
Clet age = 25
DAll of the above
Why might you prefer let over var when possible?
ABecause constants prevent accidental changes
BBecause <code>var</code> is deprecated
CBecause <code>let</code> variables use less memory
DBecause <code>let</code> variables run faster on all devices
Explain the difference between let and var in Swift and why you would choose one over the other.
Think about whether the value should stay the same or can change.
You got /5 concepts.
    Describe what type inference is and how it helps when declaring variables in Swift.
    Imagine Swift as a helpful friend who figures out the type for you.
    You got /4 concepts.