Bird
0
0

Given this Swift code, what will be the output?

hard📝 Application Q15 of 15
Swift - Control Flow

Given this Swift code, what will be the output?

let grade = "B"
switch grade {
case "A", "B", "C":
    print("Pass")
case "D", "F":
    print("Fail")
case "I", "W":
    print("Incomplete or Withdrawn")
default:
    print("Unknown grade")
}
AIncomplete or Withdrawn
BPass
CFail
DUnknown grade
Step-by-Step Solution
Solution:
  1. Step 1: Identify the value of grade

    The variable grade is "B".
  2. Step 2: Match grade in compound cases

    "B" matches the first case "A", "B", "C" which prints "Pass".
  3. Final Answer:

    Pass -> Option B
  4. Quick Check:

    "B" in first case = Pass [OK]
Quick Trick: Match value to any in compound case list [OK]
Common Mistakes:
  • Assuming "B" is a fail grade
  • Ignoring default case
  • Confusing case groups

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes