Control flow in Swift provides powerful tools to manage the execution flow of a program. Learn about conditional statements, loops, switch statements, pattern matching, and early exits for efficient programming.
1. Conditional Statements: if, else if, else, guard
What is the correct syntax for an if statement in Swift?
A) if condition { statements }
B) if (condition) then { statements }
C) if condition: { statements }
D) if condition do { statements }
What keyword is used to handle an alternative condition when the if condition is false?
A) else
B) elsif
C) default
D) otherwise
Which statement is used in Swift to exit early from a function if a condition is not met?
A) return
B) break
C) guard
D) continue
What is the purpose of a guard statement in Swift?
A) To ensure a condition is met before executing further code
B) To replace if conditions entirely
C) To handle errors explicitly
D) To create loops
Which keyword is mandatory in a guard statement to exit the scope if the condition fails?
A) else
B) return
C) break
D) defer
What happens if none of the conditions in an if-else if chain evaluates to true and there is no else block?
A) A runtime error occurs
B) The program halts
C) The code after the if chain executes
D) The default condition is triggered
In which scenarios is using guard recommended over if?
A) For concise handling of optional bindings
B) For loop iteration
C) For defining constants
D) For error handling
Can you have multiple conditions in an if statement in Swift?
A) No, only one condition is allowed
B) Yes, using && and || operators
C) Yes, by nesting if statements
D) No, you must use separate if statements
2. Loops: for-in, while, repeat-while
What type of loop is best for iterating over elements in a collection?
A) while
B) repeat-while
C) for-in
D) foreach
How does a while loop differ from a repeat-while loop in Swift?
A) while evaluates the condition before execution, repeat-while evaluates after execution
B) repeat-while cannot handle infinite loops
C) while runs at least once, repeat-while may not execute
D) They are identical
What is the syntax for a for-in loop in Swift?
A) for item in items { }
B) for (item in items) { }
C) for items -> item { }
D) for items each item { }
Which loop is guaranteed to execute its body at least once?
A) for-in
B) while
C) repeat-while
D) if
How do you loop through a range of numbers in Swift?
A) for num in range(1, 10)
B) for num from 1 to 10
C) for num in 1...10
D) for num in range[1...10]
Which of the following breaks a loop in Swift?
A) break
B) stop
C) end
D) terminate
What happens when the continue keyword is used in a loop?
A) It skips the current iteration and moves to the next
B) It ends the loop entirely
C) It pauses the loop temporarily
D) It starts the loop from the beginning
Can you iterate over a dictionary with a for-in loop in Swift?
A) No, only arrays are supported
B) Yes, using key-value pairs
C) Yes, but only the keys can be iterated
D) Yes, but only the values can be iterated
3. Switch Statements and Pattern Matching
What is the primary advantage of using switch over if in Swift?
A) It is more concise for multiple conditions
B) It executes faster
C) It allows dynamic condition evaluation
D) It works with Boolean values only
What is mandatory in a switch statement to cover all possible cases?
A) case
B) default
C) else
D) return
Which of the following is valid in a Swift switch statement?
A) Overlapping cases
B) Compound cases
C) Multiple default cases
D) Unreachable cases
What type of pattern matching does Swift’s switch statement support?
A) Only integers
B) Only strings
C) Complex patterns like ranges and tuples
D) None
Can you use switch to check multiple conditions for a single case?
A) No, only one condition per case
B) Yes, by separating conditions with commas
C) Yes, but only in if statements
D) No, Swift does not support this
What happens if a switch statement lacks a default case and does not cover all possibilities?
A) The program throws an error
B) The default is assumed
C) A warning is issued
D) It works only for exhaustive types
4. Break, Continue, and Early Exit
What does the break statement do in Swift?
A) Terminates the nearest enclosing loop or switch statement
B) Skips the current iteration of a loop
C) Exits a function
D) Stops the execution of the entire program
In which control flow structure is continue most commonly used?
A) Functions
B) Loops
C) Switch statements
D) Guards
What is an early exit in Swift?
A) Using guard to exit a function when a condition fails
B) Using return in the middle of a loop
C) Breaking out of a loop before it completes
D) Exiting a switch statement prematurely
How can you skip the current iteration in a loop in Swift?
A) Using break
B) Using continue
C) Using exit
D) Using skip
What is the effect of a break statement inside a nested loop?
A) It only exits the innermost loop
B) It exits all loops
C) It restarts the loop
D) It pauses the loop temporarily
Can a guard statement in Swift exit multiple levels of scope?
A) Yes, using multiple return statements
B) No, it can only exit one level
C) Yes, if combined with a break
D) No, guard cannot be used for exits
Which keyword is used to terminate the loop without executing further iterations?
A) stop
B) break
C) exit
D) continue
What is the purpose of an early exit in Swift?
A) To improve readability and avoid nested conditions
B) To reduce memory usage
C) To simplify loops
D) To force the program to terminate
Answers
Qno Answer 1 A) if condition { statements } 2 A) else 3 C) guard 4 A) To ensure a condition is met before executing further code 5 A) else 6 C) The code after the if chain executes 7 A) For concise handling of optional bindings 8 B) Yes, using && and ` 9 C) for-in 10 A) while evaluates the condition before execution, repeat-while evaluates after execution 11 A) for item in items { } 12 C) repeat-while 13 C) for num in 1…10 14 A) break 15 A) It skips the current iteration and moves to the next 16 B) Yes, using key-value pairs 17 A) It is more concise for multiple conditions 18 B) default 19 B) Compound cases 20 C) Complex patterns like ranges and tuples 21 B) Yes, by separating conditions with commas 22 A) The program throws an error 23 A) Terminates the nearest enclosing loop or switch statement 24 B) Loops 25 A) Using guard to exit a function when a condition fails 26 B) Using continue 27 A) It only exits the innermost loop 28 B) No, it can only exit one level 29 B) break 30 A) To improve readability and avoid nested conditions
Post Views: 47