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
intermediate2: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);
Attempts:
2 left
💡 Hint
Remember that integers are value types and copying creates a separate copy.
✗ Incorrect
In C#, int is a value type. When b is assigned a, it copies the value. Changing b later does not affect a.
❓ Predict Output
intermediate2: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);
Attempts:
2 left
💡 Hint
Classes are reference types; variables hold references to the same object.
✗ Incorrect
Box is a class, so box1 and box2 refer to the same object. Changing box2.Size changes box1.Size as well.
🔧 Debug
advanced2: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);Attempts:
2 left
💡 Hint
Structs are value types and copying creates a new independent copy.
✗ Incorrect
Point is a struct (value type). Assigning p2 = p1 copies the values. Changing p2.X does not affect p1.X.
❓ Predict Output
advanced2: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);
Attempts:
2 left
💡 Hint
The method changes the object's property, but reassigning the parameter does not affect the original reference.
✗ Incorrect
The method modifies the Value property of the object cont points to, setting it to 10. Then it reassigns the parameter c to a new object, but this does not change cont outside the method.
🧠 Conceptual
expert3:00remaining
Memory behavior of value vs reference types
Which statement best describes how value types and reference types are stored in memory in C#?
Attempts:
2 left
💡 Hint
Think about where simple data and objects live in memory.
✗ Incorrect
In C#, value types (like int, struct) are usually stored on the stack, which is fast and short-lived. Reference types (like class) are stored on the heap, and variables hold references (pointers) to these objects.