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

Switch statement execution in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Switch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
ATwo\nThree
BTwo
CThree
DDefault
Attempts:
2 left
💡 Hint
Remember that 'goto case' transfers control to another case label.
Predict Output
intermediate
2: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);
APositive integer
BNegative integer
CString
DOther
Attempts:
2 left
💡 Hint
Check the type and condition matched in the switch expression.
🔧 Debug
advanced
2: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");
}
ARuntime error: fall-through not allowed
BCompile-time error: duplicate case labels
CNo error, output is Monday\nTuesday
DCompile-time error: missing break in case 1
Attempts:
2 left
💡 Hint
C# does not allow implicit fall-through between cases.
Predict Output
advanced
2: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;
}
AAverage
BFail
CPass
DNo output
Attempts:
2 left
💡 Hint
Multiple case labels can share the same code block.
🧠 Conceptual
expert
2: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;
}
ACompile-time error
BNull value
COther number
DPositive number
Attempts:
2 left
💡 Hint
Switch supports pattern matching including null checks on nullable types.