Challenge - 5 Problems
Constant Pattern Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Look at which constant matches the variable 'number'.
✗ Incorrect
The switch expression matches the constant 5, so it returns "Five".
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Check how the null constant pattern works.
✗ Incorrect
The variable 'text' is null, so the null pattern matches and returns "Null value".
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Check which constant pattern matches the letter 'a'.
✗ Incorrect
The letter 'a' matches the first pattern with multiple constants, so it prints "Vowel".
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
Remember how nullable types match constant patterns.
✗ Incorrect
The nullable int has value 0, so it matches the constant 0 pattern and prints "Zero".
❓ Predict Output
expert2: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);
Attempts:
2 left
💡 Hint
Check which enum value matches the constant patterns.
✗ Incorrect
The current status is Running, which matches the default case, so it prints "In progress".