0
0
C Sharp (C#)programming~20 mins

Enum with switch pattern in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Enum Switch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
    }
}
ACaution
BGo
CStop
DUnknown
Attempts:
2 left
💡 Hint
Look at the switch expression matching the enum value Yellow.
Predict Output
intermediate
2: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));
    }
}
ASun shines
BCold season
CCompilation error
DFlowers bloom
Attempts:
2 left
💡 Hint
Check which case matches Season.Winter in the switch expression.
🔧 Debug
advanced
2: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));
    }
}
ACS8509: The switch expression does not handle all possible values
BSystem.InvalidOperationException at runtime
CCS0128: A local variable is already defined
DNo error, prints "Go right"
Attempts:
2 left
💡 Hint
Check if the switch expression covers all enum values.
Predict Output
advanced
2: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));
    }
}
AAuto mode ON
BManual mode ON
CManual mode OFF
DUnknown mode
Attempts:
2 left
💡 Hint
Look at the nested switch for Mode.Manual and Status.Off.
🧠 Conceptual
expert
2:00remaining
Exhaustiveness in enum switch expressions
Which statement about C# switch expressions with enums is TRUE?
AYou can only use switch statements, not switch expressions, with enums in C#.
BSwitch expressions on enums always compile even if some enum values are missing cases.
CSwitch expressions on enums do not support pattern matching.
DA switch expression on an enum must include a default case or cover all enum values to compile.
Attempts:
2 left
💡 Hint
Think about compiler checks for switch expressions on enums.