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

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Null-Conditional Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of null-conditional operator with null object
What is the output of this C# code snippet using the null-conditional operator?
C Sharp (C#)
class Program {
    static void Main() {
        string? text = null;
        int? length = text?.Length;
        System.Console.WriteLine(length == null ? "null" : length.ToString());
    }
}
Anull
BThrows NullReferenceException
C0
DEmpty string
Attempts:
2 left
💡 Hint
The null-conditional operator returns null if the object is null instead of throwing an exception.
Predict Output
intermediate
2:00remaining
Null-conditional operator with method invocation
What will this C# program print?
C Sharp (C#)
class Program {
    static string? GetName(bool returnNull) {
        if (returnNull) return null;
        return "Alice";
    }
    static void Main() {
        string? name = GetName(true);
        int? length = name?.Length;
        System.Console.WriteLine(length ?? -1);
    }
}
AThrows NullReferenceException
B5
Cnull
D-1
Attempts:
2 left
💡 Hint
The null-coalescing operator '??' provides a default value if the left side is null.
🔧 Debug
advanced
2:00remaining
Identify the error with null-conditional operator usage
What error does this C# code produce?
C Sharp (C#)
class Program {
    static void Main() {
        string? text = null;
        int length = text?.Length;
        System.Console.WriteLine(length);
    }
}
ANullReferenceException at runtime
BSyntax error: missing semicolon
CCannot implicitly convert type 'int?' to 'int'
DNo error, prints 0
Attempts:
2 left
💡 Hint
The null-conditional operator returns a nullable type, but the variable is non-nullable.
🧠 Conceptual
advanced
2:00remaining
Behavior of null-conditional operator with events
Consider this C# code snippet. What is the purpose of using the null-conditional operator when invoking the event?
C Sharp (C#)
public class Publisher {
    public event System.Action? OnChange;
    public void Raise() {
        OnChange?.Invoke();
    }
}
AIt unsubscribes all event handlers before invoking.
BIt prevents a NullReferenceException if no subscribers are attached to the event.
CIt forces the event to always have at least one subscriber.
DIt throws an exception if the event has no subscribers.
Attempts:
2 left
💡 Hint
Events can be null if no methods are subscribed.
Predict Output
expert
2:00remaining
Output with chained null-conditional operators and null-coalescing
What is the output of this C# program?
C Sharp (C#)
class Person {
    public string? Name { get; set; }
    public Person? Friend { get; set; }
}
class Program {
    static void Main() {
        Person? p = new Person { Name = "Bob", Friend = null };
        string friendName = p?.Friend?.Name ?? "No friend";
        System.Console.WriteLine(friendName);
    }
}
ANo friend
BBob
Cnull
DThrows NullReferenceException
Attempts:
2 left
💡 Hint
The null-conditional operator returns null if any part of the chain is null, then the null-coalescing operator provides a default.