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 grade = "B"
switch grade {
 case "A": print("Excellent")
 case "B", "C": print("Well done")
 case "D": print("You passed")
 default: print("Try again")
}
AWell done
BYou passed
CExcellent
DTry again
Step-by-Step Solution
Solution:
  1. Step 1: Identify the value of grade

    The variable grade is "B".
  2. Step 2: Match grade with switch cases

    Case "B", "C" matches "B", so it prints "Well done".
  3. Final Answer:

    Well done -> Option A
  4. Quick Check:

    Matching case runs = "Well done" [OK]
Quick Trick: Multiple values in case match any listed value [OK]
Common Mistakes:
  • Choosing default instead of matching case
  • Confusing case "A" with "B"
  • Assuming no output if multiple values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes