Bird
0
0

What will be the output of this Swift code?

medium📝 Predict Output Q4 of 15
Swift - Control Flow

What will be the output of this Swift code?

let status = "C"
switch status {
case "A", "B":
    print("Excellent")
case "C", "D":
    print("Good")
default:
    print("Needs Improvement")
}
AExcellent
BGood
CNeeds Improvement
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Identify the value of status

    It is "C".
  2. Step 2: Match the value in switch cases

    "C" matches the compound case "C", "D".
  3. Step 3: Determine output

    The matching case prints "Good".
  4. Final Answer:

    Good -> Option B
  5. Quick Check:

    Value "C" matches second compound case [OK]
Quick Trick: Match value to any in compound case list [OK]
Common Mistakes:
  • Assuming only first value in compound case matches
  • Confusing default with unmatched cases
  • Misreading the value assigned to variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes