Challenge - 5 Problems
Switch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of switch with fall-through using goto case
What is the output of this C# code snippet?
C Sharp (C#)
int x = 2; switch (x) { case 1: Console.WriteLine("One"); break; case 2: Console.WriteLine("Two"); goto case 3; case 3: Console.WriteLine("Three"); break; default: Console.WriteLine("Default"); break; }
Attempts:
2 left
💡 Hint
Remember that 'goto case' transfers control to another case label.
✗ Incorrect
When x is 2, it prints "Two" then jumps to case 3 and prints "Three" before breaking.
❓ Predict Output
intermediate2:00remaining
Switch expression output with pattern matching
What is the output of this C# code using a switch expression?
C Sharp (C#)
object obj = 5; string result = obj switch { int i when i > 0 => "Positive integer", int i when i < 0 => "Negative integer", string s => "String", _ => "Other" }; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
Check the type and condition matched in the switch expression.
✗ Incorrect
The object is an int with value 5, which is greater than 0, so it matches the first pattern.
🔧 Debug
advanced2:00remaining
Identify the error in switch statement
What error does this C# code produce?
C Sharp (C#)
int day = 3; switch (day) { case 1: Console.WriteLine("Monday"); case 2: Console.WriteLine("Tuesday"); break; default: Console.WriteLine("Other day"); }
Attempts:
2 left
💡 Hint
C# does not allow implicit fall-through between cases.
✗ Incorrect
C# requires each case to end with break or other jump statement unless it ends with a goto or return. Missing break causes compile error.
❓ Predict Output
advanced2:00remaining
Switch with multiple case labels and default
What is the output of this C# switch statement?
C Sharp (C#)
char grade = 'B'; switch (grade) { case 'A': case 'B': Console.WriteLine("Pass"); break; case 'C': Console.WriteLine("Average"); break; default: Console.WriteLine("Fail"); break; }
Attempts:
2 left
💡 Hint
Multiple case labels can share the same code block.
✗ Incorrect
Grade 'B' matches case 'B', which shares code with case 'A', printing "Pass".
🧠 Conceptual
expert2:00remaining
Switch statement behavior with nullable types
Given the following code, what is the output?
C Sharp (C#)
int? number = null; switch (number) { case null: Console.WriteLine("Null value"); break; case > 0: Console.WriteLine("Positive number"); break; default: Console.WriteLine("Other number"); break; }
Attempts:
2 left
💡 Hint
Switch supports pattern matching including null checks on nullable types.
✗ Incorrect
The nullable int is null, so it matches the 'case null' pattern and prints "Null value".