0
0
Swiftprogramming~20 mins

Let for constants (immutable) in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift Let Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of let constant reassignment attempt
What is the output or result of running this Swift code?
Swift
let number = 10
number = 20
print(number)
A10
B20
CCompile-time error: Cannot assign to value: 'number' is a 'let' constant
DRuntime error
Attempts:
2 left
💡 Hint
Remember that 'let' declares a constant that cannot be changed after assignment.
Predict Output
intermediate
1:30remaining
Value of a let constant after initialization
What will be printed by this Swift code?
Swift
let greeting = "Hello, world!"
print(greeting)
Agreeting
BHello, world!
CCompile-time error
DRuntime error
Attempts:
2 left
💡 Hint
Check what value is assigned to the constant 'greeting'.
🔧 Debug
advanced
2:30remaining
Why does this let constant cause a compile error?
Consider this Swift code snippet. Why does it cause a compile-time error?
Swift
let numbers = [1, 2, 3]
numbers.append(4)
print(numbers)
ABecause 'numbers' is a constant array and cannot be modified after initialization
BBecause 'append' is not a valid method for arrays
CBecause the array is empty
DBecause 'print' cannot print arrays
Attempts:
2 left
💡 Hint
Think about what 'let' means for collections like arrays.
📝 Syntax
advanced
1:30remaining
Which option correctly declares an immutable constant?
Which of the following Swift declarations correctly creates an immutable constant?
Aconst pi = 3.14159
Bvar pi = 3.14159
Cimmutable pi = 3.14159
Dlet pi = 3.14159
Attempts:
2 left
💡 Hint
Swift uses a specific keyword for constants.
🚀 Application
expert
2:00remaining
Number of items in a let constant dictionary after initialization
Given this Swift code, how many key-value pairs does the constant dictionary contain?
Swift
let capitals = ["France": "Paris", "Japan": "Tokyo", "India": "New Delhi"]
print(capitals.count)
A3
B0
C1
DCompile-time error
Attempts:
2 left
💡 Hint
Count the number of key-value pairs inside the dictionary literal.