MCQs on Control Flow | Swift

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

  1. 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 }
  2. What keyword is used to handle an alternative condition when the if condition is false?
    • A) else
    • B) elsif
    • C) default
    • D) otherwise
  3. 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
  4. 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
  5. Which keyword is mandatory in a guard statement to exit the scope if the condition fails?
    • A) else
    • B) return
    • C) break
    • D) defer
  6. 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
  7. 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
  8. 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

  1. What type of loop is best for iterating over elements in a collection?
    • A) while
    • B) repeat-while
    • C) for-in
    • D) foreach
  2. 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
  1. 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 { }
  1. Which loop is guaranteed to execute its body at least once?
  • A) for-in
  • B) while
  • C) repeat-while
  • D) if
  1. 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]
  1. Which of the following breaks a loop in Swift?
  • A) break
  • B) stop
  • C) end
  • D) terminate
  1. 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
  1. 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

  1. 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
  1. What is mandatory in a switch statement to cover all possible cases?
  • A) case
  • B) default
  • C) else
  • D) return
  1. Which of the following is valid in a Swift switch statement?
  • A) Overlapping cases
  • B) Compound cases
  • C) Multiple default cases
  • D) Unreachable cases
  1. 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
  1. 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
  1. 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

  1. 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
  1. In which control flow structure is continue most commonly used?
  • A) Functions
  • B) Loops
  • C) Switch statements
  • D) Guards
  1. 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
  1. How can you skip the current iteration in a loop in Swift?
  • A) Using break
  • B) Using continue
  • C) Using exit
  • D) Using skip
  1. 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
  1. 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
  1. Which keyword is used to terminate the loop without executing further iterations?
  • A) stop
  • B) break
  • C) exit
  • D) continue
  1. 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

QnoAnswer
1A) if condition { statements }
2A) else
3C) guard
4A) To ensure a condition is met before executing further code
5A) else
6C) The code after the if chain executes
7A) For concise handling of optional bindings
8B) Yes, using && and `
9C) for-in
10A) while evaluates the condition before execution, repeat-while evaluates after execution
11A) for item in items { }
12C) repeat-while
13C) for num in 1…10
14A) break
15A) It skips the current iteration and moves to the next
16B) Yes, using key-value pairs
17A) It is more concise for multiple conditions
18B) default
19B) Compound cases
20C) Complex patterns like ranges and tuples
21B) Yes, by separating conditions with commas
22A) The program throws an error
23A) Terminates the nearest enclosing loop or switch statement
24B) Loops
25A) Using guard to exit a function when a condition fails
26B) Using continue
27A) It only exits the innermost loop
28B) No, it can only exit one level
29B) break
30A) To improve readability and avoid nested conditions

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top