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

Constant patterns in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constant Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of constant pattern matching with int
What is the output of this C# code using constant pattern matching?
C Sharp (C#)
int number = 5;
string result = number switch
{
    1 => "One",
    5 => "Five",
    _ => "Other"
};
Console.WriteLine(result);
AOne
BCompilation error
COther
DFive
Attempts:
2 left
💡 Hint
Look at which constant matches the variable 'number'.
Predict Output
intermediate
2:00remaining
Constant pattern with string and null
What will this C# code print?
C Sharp (C#)
string? text = null;
string output = text switch
{
    null => "Null value",
    "hello" => "Greeting",
    _ => "Unknown"
};
Console.WriteLine(output);
AGreeting
BNull value
CUnknown
DRuntime exception
Attempts:
2 left
💡 Hint
Check how the null constant pattern works.
Predict Output
advanced
2:00remaining
Pattern matching with multiple constant patterns
What is the output of this code snippet?
C Sharp (C#)
char letter = 'a';
string category = letter switch
{
    'a' or 'e' or 'i' or 'o' or 'u' => "Vowel",
    'y' => "Sometimes vowel",
    _ => "Consonant"
};
Console.WriteLine(category);
AConsonant
BSometimes vowel
CVowel
DCompilation error
Attempts:
2 left
💡 Hint
Check which constant pattern matches the letter 'a'.
Predict Output
advanced
2:00remaining
Constant pattern with nullable value types
What will this code print?
C Sharp (C#)
int? number = 0;
string result = number switch
{
    0 => "Zero",
    null => "No value",
    _ => "Other"
};
Console.WriteLine(result);
AZero
BNo value
COther
DRuntime exception
Attempts:
2 left
💡 Hint
Remember how nullable types match constant patterns.
Predict Output
expert
2:00remaining
Constant pattern with enums and default case
Given this enum and code, what is the output?
C Sharp (C#)
enum Status { Ready, Running, Stopped }

Status current = Status.Running;
string message = current switch
{
    Status.Ready => "Ready to start",
    Status.Stopped => "Stopped",
    _ => "In progress"
};
Console.WriteLine(message);
AIn progress
BStopped
CCompilation error
DReady to start
Attempts:
2 left
💡 Hint
Check which enum value matches the constant patterns.