Bird
0
0

Identify the error in this Swift switch statement:

medium📝 Debug Q14 of 15
Swift - Control Flow
Identify the error in this Swift switch statement:
let grade = "B"
switch grade {
 case "A":
   print("Excellent")
 case "B"
   print("Good")
 default:
   print("Needs Improvement")
}
ASwitch cannot use strings
BMissing default case
CMissing colon after case "B"
DPrint statements must be outside switch
Step-by-Step Solution
Solution:
  1. Step 1: Check syntax of each case

    Each case label must end with a colon (:). The case "B" is missing this colon.
  2. Step 2: Confirm other parts are correct

    Default case exists, strings are allowed, and print inside switch is valid.
  3. Final Answer:

    Missing colon after case "B" -> Option C
  4. Quick Check:

    Case labels need colon = C [OK]
Quick Trick: Every case line ends with a colon : [OK]
Common Mistakes:
  • Forgetting colon after case label
  • Thinking default is optional (it is recommended)
  • Believing switch can't handle strings

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Swift Quizzes