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

Null-coalescing operator in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Null-Coalescing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
Anull
B0
CCompilation error
D5
Attempts:
2 left
💡 Hint
The null-coalescing operator returns the left value if it is not null; otherwise, it returns the right value.
Predict Output
intermediate
2:00remaining
Null-coalescing with strings
What will this program print?
C Sharp (C#)
string s = null;
string result = s ?? "default";
Console.WriteLine(result);
Adefault
Bnull
CCompilation error
DEmpty string
Attempts:
2 left
💡 Hint
If the left side is null, the operator returns the right side.
Predict Output
advanced
2: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);
Ahello
Bnull
CCompilation error
DEmpty string
Attempts:
2 left
💡 Hint
The operator evaluates left to right and returns the first non-null value.
Predict Output
advanced
2:00remaining
Null-coalescing with method call
What will this code print?
C Sharp (C#)
string? GetName() => null;

string name = GetName() ?? "Unknown";
Console.WriteLine(name);
Anull
BUnknown
CCompilation error
DEmpty string
Attempts:
2 left
💡 Hint
The method returns null, so the operator returns the fallback string.
Predict Output
expert
3: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);
ACompilation error
B0
C-1
DNullReferenceException
Attempts:
2 left
💡 Hint
The null-conditional operator returns null if 'p' is null, so the null-coalescing operator uses -1.