A nullable value type is a value type that can also hold a null value, allowing it to represent the absence of a value. It is declared using a question mark after the type, for example, int?.
Click to reveal answer
beginner
How do you declare a nullable integer in C#?
You declare a nullable integer by adding a question mark after the type: int? myNumber;. This means myNumber can hold any integer or null.
Click to reveal answer
intermediate
What does the HasValue property do in nullable types?
The HasValue property returns true if the nullable type contains a non-null value, and false if it is null.
Click to reveal answer
intermediate
How can you safely get the value of a nullable type?
You can use the Value property if HasValue is true, or use the null-coalescing operator ?? to provide a default value, for example: int value = myNumber ?? 0;.
Click to reveal answer
intermediate
What happens if you try to access the Value property of a nullable type that is null?
Accessing the Value property when the nullable type is null throws an InvalidOperationException. Always check HasValue before accessing Value.
Click to reveal answer
How do you declare a nullable double in C#?
Adouble!
Bnullable double
Cdouble?
Ddouble*
✗ Incorrect
In C#, you declare a nullable value type by adding a question mark after the type, like double?.
What does int? x = null; mean?
Ax is a string
Bx is a nullable reference type
Cx is a non-nullable integer
Dx is an integer that can be null
✗ Incorrect
The int? means x is a nullable integer, so it can hold an integer or null.
Which property tells if a nullable type has a value?
AIsEmpty
BHasValue
CValue
DIsNull
✗ Incorrect
The HasValue property returns true if the nullable type contains a value.
What does the expression myInt ?? 10 do?
AReturns myInt if not null, otherwise 10
BAlways returns 10
CThrows an error if myInt is null
DReturns null if myInt is null
✗ Incorrect
The null-coalescing operator ?? returns the left value if it is not null, otherwise the right value.
What happens if you access Value on a null nullable type?
AThrows InvalidOperationException
BReturns 0
CReturns null
DReturns default value of the type
✗ Incorrect
Accessing Value when the nullable type is null throws an InvalidOperationException.
Explain what nullable value types are and why they are useful in C#.
Think about how you might represent an empty or missing number.
You got /4 concepts.
Describe how to safely work with nullable value types to avoid exceptions.
Consider how to avoid errors when the value might be missing.
You got /3 concepts.
Practice
(1/5)
1. What does a nullable value type in C# allow you to do?
easy
A. Store only non-null values
B. Store values only in reference types
C. Store multiple values at once
D. Store a value or no value (null)
Solution
Step 1: Understand nullable value types
Nullable value types allow a value type to hold either a normal value or no value (null).
Step 2: Compare options
Only Store a value or no value (null) correctly describes this behavior; others are incorrect because value types normally cannot be null.
Final Answer:
Store a value or no value (null) -> Option D
Quick Check:
Nullable value type = value or null [OK]
Hint: Nullable means value or null, not multiple values [OK]
Common Mistakes:
Thinking nullable stores multiple values
Confusing nullable with reference types
Assuming nullable cannot hold null
2. Which of the following is the correct syntax to declare a nullable integer in C#?
easy
A. int number?;
B. int? number;
C. nullable int number;
D. Nullable number int;
Solution
Step 1: Recall nullable syntax
In C#, you add a question mark after the value type to make it nullable, like int?.
Step 2: Check each option
Only int? number; uses the correct syntax int? number;. Others are invalid syntax.
Final Answer:
int? number; -> Option B
Quick Check:
Nullable int syntax = int? [OK]
Hint: Use ? right after type for nullable, like int? [OK]
Common Mistakes:
Placing ? after variable name
Using 'nullable' keyword incorrectly
Mixing order of type and nullable
3. What is the output of this C# code?
int? x = null; int y = x ?? 5; Console.WriteLine(y);
medium
A. 0
B. null
C. 5
D. Compilation error
Solution
Step 1: Understand the null-coalescing operator
The operator ?? returns the left value if not null; otherwise, it returns the right value.
Step 2: Apply to given code
Since x is null, y gets the value 5, so Console.WriteLine(y); prints 5.
Final Answer:
5 -> Option C
Quick Check:
x is null, so y = 5 [OK]
Hint: ?? returns right value if left is null [OK]
Common Mistakes:
Assuming null prints as 0
Confusing ?? with || operator
Expecting compilation error for null assignment
4. Identify the error in this code snippet:
int? a = 10; int b = a; Console.WriteLine(b);
medium
A. Cannot assign nullable int to int without checking for null
B. Variable 'a' must be initialized
C. Console.WriteLine cannot print int values
D. No error, code runs fine
Solution
Step 1: Check assignment from nullable to non-nullable
Assigning a nullable int directly to an int variable without checking for null causes a compile error.
Step 2: Explain why error occurs
Because a might be null, C# requires explicit null check or use of .Value or null-coalescing operator.
Final Answer:
Cannot assign nullable int to int without checking for null -> Option A
Quick Check:
Nullable to int needs null check [OK]
Hint: Check nullable has value before assigning to non-nullable [OK]
Common Mistakes:
Ignoring null check before assignment
Assuming implicit conversion works
Thinking Console.WriteLine causes error
5. Given a nullable double double? d = null;, which code safely assigns its value to a non-nullable double result with default 0.0 if d is null?
hard
A. double result = d ?? 0.0;
B. double result = d.Value;
C. double result = (double)d;
D. double result = d != null ? d : null;
Solution
Step 1: Understand safe assignment from nullable
To assign a nullable to non-nullable safely, provide a default value if null using ??.
Step 2: Evaluate each option
double result = d.Value; throws exception if d is null. double result = d ?? 0.0; uses ?? to assign 0.0 if null, which is safe. double result = (double)d; casts but throws if null. double result = d != null ? d : null; tries to assign null to non-nullable, invalid.
Final Answer:
double result = d ?? 0.0; -> Option A
Quick Check:
Use ?? to provide default for nullable [OK]
Hint: Use ?? operator to assign default if nullable is null [OK]
Common Mistakes:
Using .Value without null check
Casting nullable directly without check
Assigning null to non-nullable variable
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.