Bird
Raised Fist0
C Sharp (C#)programming~10 mins

Default values for types in C Sharp (C#) - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to assign the default value of an int variable.

C Sharp (C#)
int number = [1];
Drag options to blanks, or click blank then click option'
Adefault(int)
B0
Cnull
Dint()
Attempts:
3 left
💡 Hint
Common Mistakes
Using null for value types like int causes errors.
Using 0 directly is correct but the task asks for default keyword.
2fill in blank
medium

Complete the code to assign the default value of a bool variable.

C Sharp (C#)
bool isActive = [1];
Drag options to blanks, or click blank then click option'
Atrue
Bnull
Cdefault(bool)
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using null for bool causes errors because bool is a value type.
Using true is not the default value.
3fill in blank
hard

Fix the error in the code to assign the default value of a string variable.

C Sharp (C#)
string name = [1];
Drag options to blanks, or click blank then click option'
A""
Bdefault(string)
Cnull
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 causes a type error.
Using empty string "" is not the default value.
4fill in blank
hard

Fill both blanks to create a method that returns the default value of a generic type T.

C Sharp (C#)
public T GetDefaultValue<[1]>() {
    return [2];
}
Drag options to blanks, or click blank then click option'
AT
Bdefault(T)
Cvoid
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using void as a generic type parameter is invalid.
Returning null may cause errors for value types.
5fill in blank
hard

Fill all three blanks to create a dictionary with keys as strings and values as default values of type int.

C Sharp (C#)
var defaults = new Dictionary<string, int> {
    {"one", [1],
    {"two", [2],
    {"three", [3]
};
Drag options to blanks, or click blank then click option'
Adefault(int)
B0
Cdefault
Dnull
Attempts:
3 left
💡 Hint
Common Mistakes
Using null causes errors because int is a value type.
Using 0 is correct but the task asks for default keyword.

Practice

(1/5)
1. What is the default value of an int type variable in C#?
easy
A. 0
B. null
C. 1
D. "" (empty string)

Solution

  1. Step 1: Understand default values for value types

    In C#, value types like int have a default value of zero when not initialized.
  2. Step 2: Recall default for int

    The default for int is 0, not null or any other value.
  3. Final Answer:

    0 -> Option A
  4. Quick Check:

    Default int = 0 [OK]
Hint: Remember: numbers default to zero in C# [OK]
Common Mistakes:
  • Thinking default int is null
  • Confusing default int with 1
  • Assuming default is empty string
2. Which of the following is the correct syntax to get the default value of a type T in C#?
easy
A. T.default()
B. default = T
C. default(T)
D. T.default

Solution

  1. Step 1: Recall C# syntax for default values

    The correct syntax to get the default value of a type T is default(T).
  2. Step 2: Check other options

    Options like T.default(), default = T, and T.default are invalid syntax in C#.
  3. Final Answer:

    default(T) -> Option C
  4. Quick Check:

    Use default(T) to get default value [OK]
Hint: Use default(Type) syntax to get default value [OK]
Common Mistakes:
  • Using invalid syntax like T.default()
  • Confusing assignment with default keyword
  • Trying to access default as a property
3. What will be the output of this C# code?
bool flag = default;
Console.WriteLine(flag);
medium
A. False
B. null
C. True
D. Compilation error

Solution

  1. Step 1: Understand default value of bool

    The default value of bool in C# is false.
  2. Step 2: Analyze the code output

    The variable flag is assigned default, so it holds false. Printing it outputs "False".
  3. Final Answer:

    False -> Option A
  4. Quick Check:

    Default bool = false [OK]
Hint: Default bool is always false, not true [OK]
Common Mistakes:
  • Assuming default bool is true
  • Expecting null for bool
  • Thinking code causes error
4. Identify the error in this C# code snippet:
string text = default;
int number = default;
Console.WriteLine(text.Length);
medium
A. Compile-time error on text.Length
B. No error, code runs fine
C. Syntax error on default keyword
D. NullReferenceException at runtime

Solution

  1. Step 1: Understand default for reference types

    For reference types like string, default is null.
  2. Step 2: Analyze accessing property on null

    Accessing text.Length when text is null causes a NullReferenceException at runtime.
  3. Final Answer:

    NullReferenceException at runtime -> Option D
  4. Quick Check:

    Default string = null; accessing Length causes error [OK]
Hint: Default reference types are null; accessing members causes errors [OK]
Common Mistakes:
  • Thinking default string is empty string
  • Expecting compile-time error instead of runtime
  • Ignoring null checks before member access
5. Given this struct and code:
struct Point { public int X; public int Y; }
Point p = default;
Console.WriteLine(p.X + "," + p.Y);

What is the output and why?
hard
A. Random values because struct fields are not set
B. 0,0 because struct fields default to zero
C. Compilation error because fields are uninitialized
D. null,null because struct is a reference type

Solution

  1. Step 1: Understand default for structs

    Structs are value types; their fields get default values when using default.
  2. Step 2: Default values of int fields in struct

    Both X and Y are int, so default is 0 for each.
  3. Step 3: Output concatenation

    Printing p.X + "," + p.Y outputs "0,0".
  4. Final Answer:

    0,0 because struct fields default to zero -> Option B
  5. Quick Check:

    Struct fields default to zero [OK]
Hint: Struct fields default to zero, not null [OK]
Common Mistakes:
  • Thinking structs are reference types
  • Expecting null values for struct fields
  • Assuming uninitialized fields cause errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard