Challenge - 5 Problems
Swift Variables Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate1:30remaining
What is the output of this Swift code snippet?
Consider the following Swift code:
What will be printed?
let x = 10 var y = 5 y = y + x print(y)
What will be printed?
iOS Swift
let x = 10 var y = 5 y = y + x print(y)
Attempts:
2 left
💡 Hint
Remember that 'let' defines a constant and 'var' defines a variable that can change.
✗ Incorrect
The variable y starts at 5, then we add x (which is 10) to y, so y becomes 15. The print statement outputs 15.
❓ ui_behavior
intermediate1:30remaining
What happens if you try to change a 'let' constant in Swift?
Given this code:
What will happen when you try to compile this?
let name = "Alice" name = "Bob"
What will happen when you try to compile this?
iOS Swift
let name = "Alice" name = "Bob"
Attempts:
2 left
💡 Hint
Think about what 'let' means in Swift.
✗ Incorrect
'let' declares a constant, so you cannot assign a new value after initialization. The compiler will show an error.
🧠 Conceptual
advanced1:30remaining
What type does Swift infer for this variable?
Look at this Swift code:
What type does Swift assign to 'score'?
var score = 42
What type does Swift assign to 'score'?
iOS Swift
var score = 42Attempts:
2 left
💡 Hint
Swift infers types based on the assigned value.
✗ Incorrect
Since 42 is an integer literal, Swift infers the type of 'score' as Int.
❓ lifecycle
advanced1:30remaining
What is the value of 'message' after this code runs?
Consider this Swift code:
What will be printed?
var message = "Hello" let greeting = message message = "Hi" print(greeting)
What will be printed?
iOS Swift
var message = "Hello" let greeting = message message = "Hi" print(greeting)
Attempts:
2 left
💡 Hint
Think about how assignment copies values for strings in Swift.
✗ Incorrect
The constant 'greeting' copies the value of 'message' at assignment time, so it remains "Hello" even after 'message' changes.
🔧 Debug
expert2:00remaining
Which option causes a compile-time error?
Which of these Swift code snippets will NOT compile?
Attempts:
2 left
💡 Hint
Remember that 'let' constants cannot be reassigned.
✗ Incorrect
Option D tries to assign a new value to a constant 'number', which causes a compile-time error.