Challenge - 5 Problems
Null-Coalescing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of null-coalescing with nullable int
What is the output of this C# code snippet?
C Sharp (C#)
int? a = null; int b = 5; int result = a ?? b; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
The null-coalescing operator returns the left value if it is not null; otherwise, it returns the right value.
✗ Incorrect
Since 'a' is null, the operator returns 'b', which is 5.
❓ Predict Output
intermediate2:00remaining
Null-coalescing with strings
What will this program print?
C Sharp (C#)
string s = null; string result = s ?? "default"; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
If the left side is null, the operator returns the right side.
✗ Incorrect
Since 's' is null, the expression returns "default".
❓ Predict Output
advanced2:00remaining
Chained null-coalescing operators
What is the output of this code?
C Sharp (C#)
string? a = null; string? b = null; string c = "hello"; string result = a ?? b ?? c; Console.WriteLine(result);
Attempts:
2 left
💡 Hint
The operator evaluates left to right and returns the first non-null value.
✗ Incorrect
Both 'a' and 'b' are null, so the operator returns 'c', which is "hello".
❓ Predict Output
advanced2:00remaining
Null-coalescing with method call
What will this code print?
C Sharp (C#)
string? GetName() => null; string name = GetName() ?? "Unknown"; Console.WriteLine(name);
Attempts:
2 left
💡 Hint
The method returns null, so the operator returns the fallback string.
✗ Incorrect
GetName() returns null, so the null-coalescing operator returns "Unknown".
❓ Predict Output
expert3:00remaining
Null-coalescing with nullable struct and property
What is the output of this program?
C Sharp (C#)
struct Point { public int X; public int Y; }
Point? p = null;
int x = p?.X ?? -1;
Console.WriteLine(x);Attempts:
2 left
💡 Hint
The null-conditional operator returns null if 'p' is null, so the null-coalescing operator uses -1.
✗ Incorrect
Since 'p' is null, 'p?.X' is null, so the expression evaluates to -1.