let numbers = [1, 2, 3, 4, 5]
var sum = 0for number in numbers {
sum += number
}
print(sum)
ACompilation error
B10
C0
D15
Attempts:
2 left
💡 Hint
Think about what the loop does to the sum variable.
✗ Incorrect
The loop safely iterates over each element in the array and adds it to sum. The total sum of 1+2+3+4+5 is 15.
🧠 Conceptual
intermediate
2:00remaining
Why Swift loops prevent out-of-bounds errors
Why do Swift loops like for-in prevent out-of-bounds errors when iterating over arrays?
ABecause Swift checks the array bounds automatically during iteration
BBecause Swift disables array bounds checking for performance
CBecause Swift allows accessing any index without checks
DBecause Swift requires manual index checks inside loops
Attempts:
2 left
💡 Hint
Think about how Swift protects you from common mistakes.
✗ Incorrect
Swift's for-in loop automatically iterates over valid elements only, preventing out-of-bounds access by design.
🔧 Debug
advanced
2:00remaining
Identify the runtime error in this Swift loop
What error will this Swift code produce when run?
Swift
let array = [10, 20, 30]
for i in0...array.count {
print(array[i])
}
AIndex out of range runtime error
BCompilation error due to invalid syntax
CNo error, prints all elements
DPrints all elements plus nil
Attempts:
2 left
💡 Hint
Check the range used in the loop and array indices.
✗ Incorrect
The loop uses 0...array.count which includes an index equal to array.count, causing an out-of-bounds runtime error.
📝 Syntax
advanced
2:00remaining
Which Swift loop syntax is safe and correct?
Which option shows a safe and correct way to loop through an array in Swift?
Afor i in 1..<array.count { print(array[i]) }
Bfor i in 0..<array.count { print(array[i]) }
Cfor i in 1...array.count { print(array[i]) }
Dfor i in 0...array.count { print(array[i]) }
Attempts:
2 left
💡 Hint
Remember Swift arrays start at index 0 and last index is count - 1.
✗ Incorrect
Option B uses a half-open range 0..
🚀 Application
expert
3:00remaining
Predict the output of a nested safe Swift loop
What is the output of this Swift code?
Swift
let matrix = [[1, 2], [3, 4], [5, 6]]
var total = 0for row in matrix {
for value in row {
total += value
}
}
print(total)
ACompilation error
B20
C21
DRuntime error
Attempts:
2 left
💡 Hint
Add all numbers in the nested arrays carefully.
✗ Incorrect
The nested loops safely iterate all elements in the 2D array and sum them: 1+2+3+4+5+6 = 21.
Practice
(1/5)
1. Why are Swift loops considered safe by default?
easy
A. They allow infinite loops without any checks.
B. They check conditions before each iteration to avoid errors.
C. They require manual index management to prevent errors.
D. They do not support looping over collections.
Solution
Step 1: Understand Swift loop behavior
Swift loops automatically check the loop condition before each iteration to ensure safety.
Step 2: Compare options with loop safety
Only They check conditions before each iteration to avoid errors. describes this safety feature correctly; others describe unsafe or incorrect behaviors.
Final Answer:
They check conditions before each iteration to avoid errors. -> Option B
Quick Check:
Loop safety = condition checks before iteration [OK]
Hint: Remember: Swift loops always check conditions first [OK]
Common Mistakes:
Thinking Swift loops allow infinite loops without checks
Believing manual index management is needed
Assuming loops don't support collections
2. Which of the following is the correct syntax for a safe Swift for-in loop over an array named numbers?
easy
A. loop number in numbers { print(number) }
B. for (int i = 0; i < numbers.count; i++) { print(numbers[i]) }
C. for number to numbers { print(number) }
D. for number in numbers { print(number) }
Solution
Step 1: Identify correct Swift loop syntax
Swift uses for variable in collection { } syntax for safe iteration.
Step 2: Check each option
Only for number in numbers { print(number) } matches correct syntax; A uses invalid 'loop', B is C-style (not Swift), C uses invalid 'to' keyword.
Final Answer:
for number in numbers { print(number) } -> Option D
Quick Check:
Swift for-in syntax = for number in numbers { print(number) } [OK]
Hint: Use 'for item in collection' for safe Swift loops [OK]
Common Mistakes:
Using C-style for loops in Swift
Using incorrect keywords like 'loop' or 'to'
Forgetting 'in' keyword
3. What will be the output of this Swift code?
let numbers = [1, 2, 3]
for i in 0..
medium
A. 1 2 3
B. 0 1 2
C. Error: Index out of range
D. No output
Solution
Step 1: Understand the loop range
The loop runs from 0 up to but not including numbers.count (3), so i = 0,1,2.
Step 2: Print values at each index
numbers[0] = 1, numbers[1] = 2, numbers[2] = 3; all print in order.
Final Answer:
1 2 3 -> Option A
Quick Check:
Loop prints array elements safely [OK]
Hint: Ranges like 0..
Common Mistakes:
Confusing range end as inclusive
Expecting index out of range error
Thinking loop prints indices instead of values
4. Identify the error in this Swift loop and how to fix it:
let fruits = ["apple", "banana", "cherry"]
for i in 0...fruits.count {
print(fruits[i])
}
medium
A. Using closed range causes index out of range error.
B. Missing 'in' keyword in the loop.
C. Array 'fruits' is not declared properly.
D. Loop variable 'i' should be a String.
Solution
Step 1: Analyze the loop range
The range 0...fruits.count includes fruits.count (3), but valid indices are 0 to 2.
Step 2: Identify the error and fix
Accessing fruits[3] causes index out of range error; use 0..
Final Answer:
Using closed range causes index out of range error. -> Option A
Quick Check:
Closed range includes invalid index [OK]
Hint: Use half-open range 0..
Common Mistakes:
Using closed range instead of half-open
Ignoring array index limits
Confusing loop variable type
5. You want to safely iterate over a dictionary scores to print keys and values. Which Swift loop ensures safety and prevents runtime errors?
hard
A. for i in 0...scores.count { print(scores[i]) }
B. for key in scores.keys { print(scores[key]) }
C. for (key, value) in scores { print(key, value) }
D. while true { print(scores) }
Solution
Step 1: Understand dictionary iteration in Swift
Swift allows safe iteration over dictionaries using for (key, value) in dictionary.
Step 2: Evaluate options for safety
for (key, value) in scores { print(key, value) } uses correct syntax and safely accesses keys and values. for i in 0...scores.count { print(scores[i]) } is invalid for dictionaries, C risks optional unwrapping, D causes infinite loop.
Final Answer:
for (key, value) in scores { print(key, value) } -> Option C
Quick Check:
Safe dictionary loop = for (key, value) in scores { print(key, value) } [OK]
Hint: Use for (key, value) in dictionary for safe iteration [OK]
Common Mistakes:
Using index-based loops on dictionaries
Forgetting optional unwrapping for dictionary values
Creating infinite loops unintentionally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.