What is the output of this Swift code?
let x = 10 var y = 20 y = y + 5 print(x, y)
Remember that let creates a constant and var creates a variable.
x is a constant with value 10 and cannot change. y is a variable initially 20, then increased by 5 to 25. So the output is 10 25.
What error does this Swift code produce?
let a = 5 a = 10 print(a)
Think about whether constants can be changed after assignment.
You cannot assign a new value to a constant declared with let. The compiler will stop with an error.
Which reason best explains why using let is preferred when possible?
Think about safety and bugs in your code.
Using let prevents accidental changes to values, making code safer and easier to understand.
What is the output of this Swift code?
var count = 1 func increment() { count += 1 } increment() print(count)
Consider how var variables can be changed inside functions.
The variable count starts at 1, the function adds 1, so the output is 2.
Why does this Swift code fail to compile?
func updateValue() { let number = 10 number += 5 print(number) } updateValue()
Look at how number is declared and then changed.
The constant number declared with let cannot be changed with +=. This causes a compile error.