Challenge - 5 Problems
Swift Let Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that 'let' declares a constant that cannot be changed after assignment.
✗ Incorrect
In Swift, variables declared with 'let' are constants and cannot be reassigned. Trying to assign a new value to 'number' causes a compile-time error.
❓ Predict Output
intermediate1:30remaining
Value of a let constant after initialization
What will be printed by this Swift code?
Swift
let greeting = "Hello, world!" print(greeting)
Attempts:
2 left
💡 Hint
Check what value is assigned to the constant 'greeting'.
✗ Incorrect
The constant 'greeting' is assigned the string "Hello, world!" and printing it outputs that string.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Think about what 'let' means for collections like arrays.
✗ Incorrect
Declaring an array with 'let' makes the array itself immutable. You cannot add or remove elements. To modify the array, declare it with 'var'.
📝 Syntax
advanced1:30remaining
Which option correctly declares an immutable constant?
Which of the following Swift declarations correctly creates an immutable constant?
Attempts:
2 left
💡 Hint
Swift uses a specific keyword for constants.
✗ Incorrect
'let' is the correct keyword to declare constants in Swift. 'var' declares variables. 'const' and 'immutable' are not valid Swift keywords.
🚀 Application
expert2: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)
Attempts:
2 left
💡 Hint
Count the number of key-value pairs inside the dictionary literal.
✗ Incorrect
The dictionary 'capitals' has three key-value pairs, so its count is 3.