Bird
0
0

What will be the output of the following Swift code?

medium📝 Predict Output Q13 of 15
Swift - Control Flow
What will be the output of the following Swift code?
let number = 3
switch number {
 case 1, 3, 5:
   print("Odd")
 case 2, 4, 6:
   print("Even")
 default:
   print("Unknown")
}
AOdd
BEven
CUnknown
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Check the value of number

    The variable number is set to 3.
  2. Step 2: Match number in switch cases

    Case 1, 3, 5 matches number 3, so it prints "Odd".
  3. Final Answer:

    Odd -> Option A
  4. Quick Check:

    3 in (1,3,5) = Odd [OK]
Quick Trick: Look for matching case values separated by commas [OK]
Common Mistakes:
  • Choosing Even because 3 is odd but not in even case
  • Ignoring multiple values in one case
  • Assuming default runs when a case matches

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes