0
0
Swiftprogramming~20 mins

For-in with where clause in Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Swift For-in Where Clause Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a Swift for-in loop with where clause filtering even numbers
What will be printed by this Swift code?
let numbers = [1, 2, 3, 4, 5, 6]
for num in numbers where num % 2 == 0 {
    print(num)
}
Swift
let numbers = [1, 2, 3, 4, 5, 6]
for num in numbers where num % 2 == 0 {
    print(num)
}
A
2
4
6
B
1
3
5
C
1
2
3
4
5
6
DNo output, syntax error
Attempts:
2 left
💡 Hint
The where clause filters elements before the loop body runs.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Swift for-in loop with where clause
Which option correctly fixes the syntax error in this code?
let fruits = ["apple", "banana", "cherry"]
for fruit in fruits where fruit.hasPrefix("a")
    print(fruit)
Swift
let fruits = ["apple", "banana", "cherry"]
for fruit in fruits where fruit.hasPrefix("a")
    print(fruit)
A
Remove the where clause:
for fruit in fruits {
    print(fruit)
}
B
Change 'where' to 'if':
for fruit in fruits if fruit.hasPrefix("a") {
    print(fruit)
}
C
Add a colon after the where clause:
for fruit in fruits where fruit.hasPrefix("a"): {
    print(fruit)
}
D
Add curly braces around the loop body:
for fruit in fruits where fruit.hasPrefix("a") {
    print(fruit)
}
Attempts:
2 left
💡 Hint
Swift requires braces for loop bodies with multiple lines or where clauses.
optimization
advanced
2:00remaining
Optimize filtering in a Swift for-in loop with where clause
Given this code:
let numbers = Array(1...1000)
var sum = 0
for num in numbers where num % 5 == 0 {
    sum += num
}

Which option is the most efficient way to calculate the sum of multiples of 5?
Swift
let numbers = Array(1...1000)
var sum = 0
for num in numbers where num % 5 == 0 {
    sum += num
}
A
Use stride to iterate only multiples of 5:
for num in stride(from: 5, through: 1000, by: 5) {
    sum += num
}
B
Filter first then loop:
for num in numbers.filter({ $0 % 5 == 0 }) {
    sum += num
}
CKeep the original code as it is the most efficient.
DUse a while loop to check each number and add if divisible by 5.
Attempts:
2 left
💡 Hint
Avoid checking every number when you can jump directly to multiples.
🔧 Debug
advanced
2:00remaining
Debug why this Swift for-in loop with where clause prints no output
Consider this code:
let words = ["Swift", "is", "fun"]
for word in words where word.count > 10 {
    print(word)
}

Why does it print nothing?
Swift
let words = ["Swift", "is", "fun"]
for word in words where word.count > 10 {
    print(word)
}
AThe print statement is outside the loop body due to missing braces.
BThe syntax of the where clause is incorrect, causing the loop to skip printing.
CNo word in the array has more than 10 characters, so the where clause filters out all elements.
DThe array is empty, so the loop never runs.
Attempts:
2 left
💡 Hint
Check the condition in the where clause against the array elements.
🧠 Conceptual
expert
3:00remaining
Understanding the behavior of multiple where clauses in nested for-in loops
What will be the output of this Swift code?
let numbers = [1, 2, 3]
let letters = ["a", "b", "c"]
for num in numbers where num % 2 != 0 {
    for letter in letters where letter != "b" {
        print("\(num)\(letter)")
    }
}
Swift
let numbers = [1, 2, 3]
let letters = ["a", "b", "c"]
for num in numbers where num % 2 != 0 {
    for letter in letters where letter != "b" {
        print("\(num)\(letter)")
    }
}
A
1a
1b
1c
3a
3b
3c
B
1a
1c
3a
3c
C
2a
2c
DNo output due to syntax error
Attempts:
2 left
💡 Hint
Both loops filter elements with their own where clauses.