0
0
iOS Swiftmobile~20 mins

Variables (let, var) and type inference in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Variables Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
📝 Syntax
intermediate
1:30remaining
What is the output of this Swift code snippet?
Consider the following Swift code:
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)
A10
B15
C5
DCompilation error
Attempts:
2 left
💡 Hint
Remember that 'let' defines a constant and 'var' defines a variable that can change.
ui_behavior
intermediate
1:30remaining
What happens if you try to change a 'let' constant in Swift?
Given this code:
let name = "Alice"
name = "Bob"

What will happen when you try to compile this?
iOS Swift
let name = "Alice"
name = "Bob"
ARuntime error when assigning "Bob"
BThe value of 'name' changes to "Bob"
CCompilation error because 'name' is a constant
DThe code runs but prints "Alice"
Attempts:
2 left
💡 Hint
Think about what 'let' means in Swift.
🧠 Conceptual
advanced
1:30remaining
What type does Swift infer for this variable?
Look at this Swift code:
var score = 42

What type does Swift assign to 'score'?
iOS Swift
var score = 42
AInt
BDouble
CString
DAny
Attempts:
2 left
💡 Hint
Swift infers types based on the assigned value.
lifecycle
advanced
1:30remaining
What is the value of 'message' after this code runs?
Consider this Swift code:
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)
AHello
BHi
CCompilation error
DRuntime error
Attempts:
2 left
💡 Hint
Think about how assignment copies values for strings in Swift.
🔧 Debug
expert
2:00remaining
Which option causes a compile-time error?
Which of these Swift code snippets will NOT compile?
A
var text = "Hello"
text = "World"
Blet pi = 3.14
C
var flag: Bool
flag = true
D
let number: Int = 10
number = 20
Attempts:
2 left
💡 Hint
Remember that 'let' constants cannot be reassigned.