Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using an enum in C#?
easy
A. To give meaningful names to a set of related constant values
B. To create a new class with methods
C. To store multiple unrelated variables
D. To define a method that returns multiple values

Solution

  1. Step 1: Understand what enums represent

    Enums are used to name a fixed set of related constants, like days of the week or colors.
  2. Step 2: Compare options with enum purpose

    Only To give meaningful names to a set of related constant values correctly describes enums as naming related constant values.
  3. Final Answer:

    To give meaningful names to a set of related constant values -> Option A
  4. Quick Check:

    Enum = Named constants [OK]
Hint: Enums name fixed sets of values clearly [OK]
Common Mistakes:
  • Thinking enums create classes
  • Confusing enums with variables
  • Assuming enums store unrelated data
2. Which of the following is the correct syntax to declare an enum named Day with values Monday, Tuesday, and Wednesday?
easy
A. enum Day (Monday, Tuesday, Wednesday);
B. enum Day { Monday, Tuesday, Wednesday }
C. enum Day: Monday, Tuesday, Wednesday;
D. enum Day = { Monday, Tuesday, Wednesday };

Solution

  1. Step 1: Recall enum declaration syntax

    In C#, enums are declared with the keyword enum, name, and curly braces listing values separated by commas.
  2. Step 2: Match syntax to options

    enum Day { Monday, Tuesday, Wednesday } matches the correct syntax: enum Day { Monday, Tuesday, Wednesday }.
  3. Final Answer:

    enum Day { Monday, Tuesday, Wednesday } -> Option B
  4. Quick Check:

    Enum syntax uses braces and commas [OK]
Hint: Use braces and commas to list enum values [OK]
Common Mistakes:
  • Using equals sign (=) after enum name
  • Using colons or parentheses incorrectly
  • Missing commas between values
3. Given the enum and switch code below, what will be printed?
enum Color { Red, Green, Blue }

Color c = Color.Green;
switch (c)
{
    case Color.Red:
        Console.WriteLine("Stop");
        break;
    case Color.Green:
        Console.WriteLine("Go");
        break;
    case Color.Blue:
        Console.WriteLine("Wait");
        break;
}
medium
A. Go
B. Stop
C. Wait
D. No output

Solution

  1. Step 1: Identify the enum value assigned

    The variable c is assigned Color.Green.
  2. Step 2: Follow switch cases for Color.Green

    The switch matches Color.Green and prints "Go".
  3. Final Answer:

    Go -> Option A
  4. Quick Check:

    Switch on enum Green prints "Go" [OK]
Hint: Match enum value to switch case output [OK]
Common Mistakes:
  • Confusing enum values with strings
  • Missing break causing fall-through
  • Assuming default case runs without match
4. What is wrong with the following code snippet?
enum Status { Pending, Approved, Rejected }

Status s = Status.Approved;
switch (s)
{
    case Status.Pending:
        Console.WriteLine("Waiting");
    case Status.Approved:
        Console.WriteLine("Accepted");
        break;
    case Status.Rejected:
        Console.WriteLine("Denied");
        break;
}
medium
A. Variable s is not assigned a value
B. Enum values are not declared correctly
C. Switch statement syntax is invalid
D. Missing break after case Status.Pending causes fall-through

Solution

  1. Step 1: Check switch cases for breaks

    The case Status.Pending lacks a break;, so execution falls through to the next case.
  2. Step 2: Understand fall-through effect

    If s was Status.Pending, it would print both "Waiting" and "Accepted" unintentionally.
  3. Final Answer:

    Missing break after case Status.Pending causes fall-through -> Option D
  4. Quick Check:

    Each switch case needs a break [OK]
Hint: Always add break after each switch case [OK]
Common Mistakes:
  • Forgetting break causes multiple case runs
  • Assuming enum declaration is wrong
  • Thinking variable is uninitialized
5. You want to write a method that takes an enum Season with values Spring, Summer, Fall, Winter and returns a string describing the typical weather. Which switch expression correctly returns "Warm" for Summer and Spring, "Cool" for Fall, and "Cold" for Winter?
hard
A. return season switch { Season.Spring, Season.Summer => "Warm", Season.Fall => "Cold", Season.Winter => "Cool" };
B. switch (season) { case Season.Spring: case Season.Summer: return "Warm"; case Season.Fall: return "Cool"; case Season.Winter: return "Cold"; default: return "Unknown"; }
C. return season switch { Season.Spring => "Warm", Season.Summer => "Warm", Season.Fall => "Cool", Season.Winter => "Cold", _ => "Unknown" };
D. switch (season) { case Season.Spring: return "Warm"; case Season.Summer: return "Cool"; case Season.Fall: return "Cold"; case Season.Winter: return "Cold"; }

Solution

  1. Step 1: Check switch expression syntax

    return season switch { Season.Spring => "Warm", Season.Summer => "Warm", Season.Fall => "Cool", Season.Winter => "Cold", _ => "Unknown" }; uses the modern C# switch expression with correct syntax and covers all enum values with correct returns.
  2. Step 2: Verify correct mapping of seasons to weather

    Spring and Summer return "Warm", Fall returns "Cool", Winter returns "Cold", and a default case handles unknowns.
  3. Final Answer:

    return season switch { Season.Spring => "Warm", Season.Summer => "Warm", Season.Fall => "Cool", Season.Winter => "Cold", _ => "Unknown" }; -> Option C
  4. Quick Check:

    Switch expression with correct mappings [OK]
Hint: Use switch expressions for concise enum mapping [OK]
Common Mistakes:
  • Using incorrect syntax for multiple values in switch expression
  • Mixing return values incorrectly
  • Missing default case in switch expression
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard