0
0
Swiftprogramming~5 mins

Let for constants (immutable) in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the let keyword do in Swift?

The let keyword declares a constant, which means its value cannot be changed after it is set.

Click to reveal answer
beginner
What happens if you try to change a let constant after it is set?

Swift will give a compile-time error because constants cannot be modified once assigned.

Click to reveal answer
beginner
How do let and var differ in Swift?

let declares a constant (immutable), while var declares a variable (mutable).

Click to reveal answer
beginner
Example: <br><code>let pi = 3.14</code><br>Can you change <code>pi</code> later in the code?

No, because pi is declared with let, it is a constant and cannot be changed.

Click to reveal answer
beginner
Why use let instead of var when possible?

Using let helps prevent accidental changes and makes your code safer and easier to understand.

Click to reveal answer
What keyword declares an immutable value in Swift?
Alet
Bvar
Cconst
Dimmutable
What happens if you try to assign a new value to a let constant?
AThe program runs normally
BThe value changes silently
CRuntime error
DCompile-time error
Which keyword allows you to change the value after declaration?
Avar
Bconst
Clet
Dfinal
Why is it good to use let when possible?
ATo make code faster
BTo prevent accidental changes
CTo allow changes later
DTo save memory
Which of these is a correct constant declaration in Swift?
Aimmutable name = "Alice"
Bvar name = "Alice"
Clet name = "Alice"
Dconst name = "Alice"
Explain what let does in Swift and why it is useful.
Think about how constants help avoid mistakes.
You got /4 concepts.
    Describe the difference between let and var in Swift.
    One can change, the other cannot.
    You got /4 concepts.