Find out if you understand how control flow works in Swift with this quiz.
1.
Which of the following describes control flow in programming?
2.
What are the two primary types of control flow in Swift?
3.
Which of the following is the correct syntax for a for-in loop in Swift?
4.
In the following Swift for-in loop, what is the role of the index constant?
for index in 1...5 {
print("Value of index is \(index)")
}
5.
What is the purpose of using an underscore (_) in a for-in loop in Swift?
6.
When is a while loop preferred over a for-in loop in Swift?
7.
What will the following code output?
var count = 0
while count < 3 {
count += 1
print("Count is \(count)")
}
8.
What differentiates a repeat…while loop from a while loop in Swift?
9.
Which of the following is the correct syntax for a repeat…while loop in Swift?
10.
In the following code, how many times will the loop execute?
var i = 3
repeat {
print(i)
i -= 1
} while i > 0
11.
Which Swift statement allows breaking out of a loop before its completion criteria are met?
12.
What will the following code output?
var j = 10
for _ in 0..<5 {
j += j
if j > 50 {
break
}
print("j = \(j)")
}
13.
Which Swift statement causes the remaining code in a loop iteration to be skipped and the next iteration to begin?
14.
What will the following code output?
var i = 1
while i < 5 {
i += 1
if i % 2 != 0 {
continue
}
print("i = \(i)")
}
15.
Which of the following describes the primary use of the if statement in Swift?
16.
Which of the following is the correct syntax for an if…else statement in Swift?
17.
In Swift, what are the main benefits of using a switch statement over multiple if…else statements?
18.
Which of the following best describes the guard statement in Swift?
19.
What must a guard statement in Swift always include?
20.
What will the following function output when called with multiplyByTen(value: 10)?
func multiplyByTen(value: Int?) {
guard let number = value, number < 10 else {
print("Number is too high")
return
}
let result = number * 10
print(result)
}
21.
What is the key difference between using guard and if for optional binding in Swift?
22.
What is the primary purpose of the continue statement in Swift loops?
Congratulations on completing the Swift Control Flow Quiz.
Click the Submit button to review your results.
Enter your email address below if you would like to receive a copy of your test results.