Complete the code to create a safe loop that iterates over an array.
let numbers = [1, 2, 3, 4, 5] for number in [1] { print(number) }
Using the array name directly in the loop ensures safe iteration over its elements.
Complete the code to safely iterate over the indices of an array.
let fruits = ["Apple", "Banana", "Cherry"] for index in [1] { print(fruits[index]) }
Using 0..
Fix the error in the loop to prevent out-of-bounds access.
let letters = ["a", "b", "c"] for i in 0...[1] { print(letters[i]) }
The last valid index is letters.count - 1; using this prevents out-of-bounds errors.
Fill both blanks to create a dictionary comprehension that filters keys with values greater than 10.
let data = ["a": 5, "b": 15, "c": 20] let filtered = data.filter { $0.[1] > [2] }
Accessing the value with $0.value and comparing it to 10 filters correctly.
Fill all three blanks to create a safe loop that prints uppercase keys with values less than 50.
let scores = ["math": 45, "science": 55, "english": 40] for ([1], [2]) in scores { if [3] < 50 { print([1].uppercased()) } }
Using (subject, score) and checking score < 50 prints uppercase subject names safely.
