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

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

Choose your learning style9 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.