Consider this Swift code snippet. What will it print?
var count = 5 count = 10 print(count)
Remember, var allows the value to change.
The variable count is declared with var, so it can be changed from 5 to 10. The print statement outputs 10.
What error will this Swift code produce?
let name = "Alice" name = "Bob" print(name)
let means the value cannot be changed after assignment.
Since name is declared with let, trying to assign a new value causes a compile-time error.
Examine this Swift code. Why does it fail to compile?
var number = 10 number = "ten" print(number)
Swift variables have fixed types once declared.
The variable number is declared as an Int. Assigning a String causes a type mismatch compile error.
Choose the best explanation of the difference between var and let in Swift.
Think about whether the value can change after assignment.
var allows the value to be changed later. let means the value cannot be changed once set.
What will this Swift code print?
var x = 5 if true { var x = 10 print(x) } print(x)
Remember that variables declared inside a block have their own scope.
The inner var x = 10 shadows the outer x inside the if block. So first print outputs 10, then outside the block it prints 5.