Challenge - 5 Problems
Enum Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Enum switch expression
What is the output of the following C# code?
C Sharp (C#)
enum TrafficLight { Red, Yellow, Green } class Program { static string GetAction(TrafficLight light) => light switch { TrafficLight.Red => "Stop", TrafficLight.Yellow => "Caution", TrafficLight.Green => "Go", _ => "Unknown" }; static void Main() { System.Console.WriteLine(GetAction(TrafficLight.Yellow)); } }
Attempts:
2 left
💡 Hint
Look at the switch expression matching the enum value Yellow.
✗ Incorrect
The switch expression matches TrafficLight.Yellow and returns "Caution".
❓ Predict Output
intermediate2:00remaining
Switch expression with default case output
What will this C# program print when run?
C Sharp (C#)
enum Season { Spring, Summer, Autumn, Winter } class Program { static string Describe(Season s) => s switch { Season.Spring => "Flowers bloom", Season.Summer => "Sun shines", _ => "Cold season" }; static void Main() { System.Console.WriteLine(Describe(Season.Winter)); } }
Attempts:
2 left
💡 Hint
Check which case matches Season.Winter in the switch expression.
✗ Incorrect
Winter is not explicitly matched, so the default case '_' returns "Cold season".
🔧 Debug
advanced2:00remaining
Identify the error in enum switch pattern
What error will this C# code produce when compiled?
C Sharp (C#)
enum Direction { North, East, South, West } class Program { static string Turn(Direction d) => d switch { Direction.North => "Go up", Direction.East => "Go right", Direction.South => "Go down" }; static void Main() { System.Console.WriteLine(Turn(Direction.West)); } }
Attempts:
2 left
💡 Hint
Check if the switch expression covers all enum values.
✗ Incorrect
The switch expression misses Direction.West and has no default case, causing CS8509 compile error.
❓ Predict Output
advanced2:00remaining
Output of nested switch with enum
What is the output of this C# program?
C Sharp (C#)
enum Mode { Auto, Manual } enum Status { On, Off } class Program { static string GetMessage(Mode m, Status s) => m switch { Mode.Auto => s switch { Status.On => "Auto mode ON", Status.Off => "Auto mode OFF" }, Mode.Manual => s switch { Status.On => "Manual mode ON", Status.Off => "Manual mode OFF" }, _ => "Unknown mode" }; static void Main() { System.Console.WriteLine(GetMessage(Mode.Manual, Status.Off)); } }
Attempts:
2 left
💡 Hint
Look at the nested switch for Mode.Manual and Status.Off.
✗ Incorrect
For Mode.Manual and Status.Off, the nested switch returns "Manual mode OFF".
🧠 Conceptual
expert2:00remaining
Exhaustiveness in enum switch expressions
Which statement about C# switch expressions with enums is TRUE?
Attempts:
2 left
💡 Hint
Think about compiler checks for switch expressions on enums.
✗ Incorrect
C# requires switch expressions on enums to be exhaustive by covering all enum values or having a default case.