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

Value types vs reference types mental model in C Sharp (C#) - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Value and Reference Types
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Understanding value type assignment
What is the output of this C# code snippet?
C Sharp (C#)
int a = 5;
int b = a;
b = 10;
Console.WriteLine(a);
ACompilation error
B5
C0
D10
Attempts:
2 left
💡 Hint
Remember that integers are value types and copying creates a separate copy.
❓ Predict Output
intermediate
2:00remaining
Reference type assignment behavior
What will this C# program print?
C Sharp (C#)
class Box { public int Size; }

Box box1 = new Box() { Size = 3 };
Box box2 = box1;
box2.Size = 7;
Console.WriteLine(box1.Size);
A7
B3
C0
DCompilation error
Attempts:
2 left
💡 Hint
Classes are reference types; variables hold references to the same object.
🔧 Debug
advanced
2:30remaining
Why does this struct behave unexpectedly?
Consider this C# code. What is the output and why? struct Point { public int X; public int Y; } Point p1 = new Point() { X = 1, Y = 2 }; Point p2 = p1; p2.X = 5; Console.WriteLine(p1.X);
C Sharp (C#)
struct Point { public int X; public int Y; }

Point p1 = new Point() { X = 1, Y = 2 };
Point p2 = p1;
p2.X = 5;
Console.WriteLine(p1.X);
ACompilation error
B5
C1
D0
Attempts:
2 left
💡 Hint
Structs are value types and copying creates a new independent copy.
❓ Predict Output
advanced
2:30remaining
Effect of modifying a reference type inside a method
What will this C# code print?
C Sharp (C#)
class Container { public int Value; }

void Modify(Container c) {
  c.Value = 10;
  c = new Container() { Value = 20 };
}

Container cont = new Container() { Value = 5 };
Modify(cont);
Console.WriteLine(cont.Value);
ACompilation error
B20
C5
D10
Attempts:
2 left
💡 Hint
The method changes the object's property, but reassigning the parameter does not affect the original reference.
🧠 Conceptual
expert
3:00remaining
Memory behavior of value vs reference types
Which statement best describes how value types and reference types are stored in memory in C#?
AValue types are stored on the stack; reference types are stored on the heap with variables holding references.
BValue types are stored on the heap; reference types are stored on the stack with variables holding values.
CBoth value and reference types are always stored on the heap.
DBoth value and reference types are always stored on the stack.
Attempts:
2 left
💡 Hint
Think about where simple data and objects live in memory.

Practice

(1/5)
1. Which of the following best describes how value types behave in C#?
easy
A. They store a reference to the data, so copying shares the same data.
B. They cannot be assigned to variables.
C. They always require manual memory management.
D. They store the actual data directly, and copying creates independent copies.

Solution

  1. Step 1: Understand value type storage

    Value types store the actual data directly in the variable's memory space.
  2. Step 2: Understand copying behavior

    When you copy a value type, a new independent copy of the data is created, so changes to one do not affect the other.
  3. Final Answer:

    They store the actual data directly, and copying creates independent copies. -> Option D
  4. Quick Check:

    Value types = independent copies [OK]
Hint: Value types copy data directly, no shared references [OK]
Common Mistakes:
  • Thinking value types share data like reference types
  • Confusing value types with reference types
  • Believing value types require manual memory management
2. Which of the following is the correct way to declare and initialize an integer variable to 5 (a value type) in C#?
easy
A. int number = 5;
B. int number = "five";
C. int number = null;
D. int number = new int();

Solution

  1. Step 1: Check valid integer assignment

    In C#, you can assign an integer value directly using syntax like int number = 5;.
  2. Step 2: Identify invalid options

    int number = new int(); is valid but unnecessarily verbose; int number = null; is invalid because value types cannot be null without nullable syntax; int number = "five"; assigns a string to an int, which is invalid.
  3. Final Answer:

    int number = 5; -> Option A
  4. Quick Check:

    Direct assignment of int value [OK]
Hint: Assign value types directly with literals like int x = 5 [OK]
Common Mistakes:
  • Trying to assign null to a non-nullable value type
  • Assigning string values to integers
  • Using new keyword unnecessarily for value types
3. What will be the output of the following C# code?
int a = 10;
int b = a;
b = 20;
Console.WriteLine(a);
medium
A. 0
B. 20
C. 10
D. Compilation error

Solution

  1. Step 1: Understand value type copying

    Variable b is assigned the value of a, so b gets a copy of 10.
  2. Step 2: Changing b does not affect a

    When b is changed to 20, a remains unchanged because they are independent copies.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    Value types copy data, original unchanged [OK]
Hint: Changing copy of value type doesn't affect original [OK]
Common Mistakes:
  • Assuming a changes when b changes
  • Confusing value types with reference types
  • Expecting runtime or compile errors
4. Consider this code snippet:
class Person { public string Name; }
Person p1 = new Person();
p1.Name = "Alice";
Person p2 = p1;
p2.Name = "Bob";
Console.WriteLine(p1.Name);

What is the output and why?
medium
A. "Bob" because p1 and p2 reference the same object
B. "Alice" because p1 and p2 are independent copies
C. Compilation error due to missing constructor
D. "Alice" because p2 is a value type copy

Solution

  1. Step 1: Identify reference type behavior

    Person is a class, so p1 and p2 hold references to the same object in memory.
  2. Step 2: Changing p2.Name affects p1.Name

    Since both variables point to the same object, changing p2.Name to "Bob" changes p1.Name as well.
  3. Final Answer:

    "Bob" because p1 and p2 reference the same object -> Option A
  4. Quick Check:

    Reference types share data, changes reflect [OK]
Hint: Classes are reference types; copies share same object [OK]
Common Mistakes:
  • Thinking classes behave like value types
  • Expecting independent copies for reference types
  • Confusing compilation errors with runtime behavior
5. You want to create a method that swaps two integers in C#. Given that integers are value types, which approach correctly swaps their values outside the method?
hard
A. Pass integers normally and swap inside method; changes reflect outside.
B. Pass integers by reference using the ref keyword and swap inside method.
C. Pass integers as objects and swap their references inside method.
D. Use a class wrapper for integers and swap the wrapper instances.

Solution

  1. Step 1: Understand value type parameter passing

    By default, value types are passed by value, so swapping inside a method won't affect the original variables.
  2. Step 2: Use ref keyword to pass by reference

    Passing integers with ref allows the method to modify the original variables directly, enabling swapping.
  3. Final Answer:

    Pass integers by reference using the ref keyword and swap inside method. -> Option B
  4. Quick Check:

    Use ref to modify value types outside method [OK]
Hint: Use ref keyword to swap value types outside method [OK]
Common Mistakes:
  • Assuming normal parameter passing swaps original values
  • Trying to swap references of value types directly
  • Using class wrappers unnecessarily
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