yoklainterview sim

Backend Csharp Memory Performance Interview Questions

75 verified Backend Csharp Memory Performance interview questions — solve with answers, learn from explanations, test yourself in a real simulation.

Try the real simulation →

Sample questions

Csharp Memory PerformanceDifficulty 1
In C#, which of these is a value type by default?
  • aclass Point { }
  • bstruct Point { }
  • cstring
  • dobject
Explanation:struct declares a value type in C# — instances are stored inline (on the stack, or inline inside whatever contains them) and are copied by value on assignment. class, string, and object are all reference types: variables of those types hold a reference to data on the heap.
Csharp Memory PerformanceDifficulty 2
struct Point
{
    public int X;
    public int Y;
}

var p1 = new Point { X = 1, Y = 2 };
var p2 = p1;
p2.X = 99;

Console.WriteLine(p1.X);

What does this print?
  • a1
  • b99
  • c0
  • dIt throws a NullReferenceException
Explanation:Point is a struct (value type), so var p2 = p1; copies the entire value into p2p2 is an independent copy, not a reference to p1. Mutating p2.X has no effect on p1, so p1.X is still 1.
Csharp Memory PerformanceDifficulty 2
class Box
{
    public int Value;
}

var b1 = new Box { Value = 1 };
var b2 = b1;
b2.Value = 99;

Console.WriteLine(b1.Value);

What does this print?
  • a1
  • b99
  • c0
  • dIt doesn't compile
Explanation:Box is a class (reference type). var b2 = b1; copies only the reference, so b1 and b2 both point to the same object on the heap. Changing b2.Value changes the same underlying object that b1 refers to, so b1.Value is also 99.
Csharp Memory PerformanceDifficulty 3
int number = 42;
object boxed = number;
int unboxed = (int)boxed;

Console.WriteLine(unboxed);

What does the line object boxed = number; do?
  • aIt creates a reference to number directly, no copy is made
  • bIt fails to compile because int cannot be assigned to object
  • cIt allocates a new object on the heap and copies the value of number into it (boxing)
  • dIt converts number to a string representation
Explanation:Assigning a value type to an object (or any interface it implements) triggers boxing: the CLR allocates a new object on the managed heap and copies the value type's data into it. The original number variable and the boxed copy are then independent — this is why boxing has a real allocation and copy cost.
Csharp Memory PerformanceDifficulty 2
What is unboxing in C#?
  • aRemoving a variable from scope so the garbage collector can reclaim it
  • bConverting a class instance into a struct
  • cDeclaring a variable without initializing it
  • dExtracting the value type back out of a boxed object (e.g. casting object back to int)
Explanation:Unboxing is the reverse of boxing: taking a reference stored as object (which holds a boxed value type) and casting it back to the original value type, e.g. int i = (int)boxedObj;. This copies the value out of the heap-allocated box into a value-type variable; an invalid cast throws InvalidCastException.
Csharp Memory PerformanceDifficulty 3
A junior developer writes a loop that adds 100,000 int values to a non-generic System.Collections.ArrayList. A senior colleague says this will be much slower than using List<int>. Why?
  • aArrayList stores object references, so each int is boxed on insertion
  • bArrayList has a fixed maximum capacity of 10,000 elements and will throw past that
  • cArrayList is thread-safe by default, and the resulting locking overhead slows every operation down
  • dArrayList sorts elements automatically on every Add call
Explanation:ArrayList is untyped and stores everything as object. Adding an int boxes it (heap allocation + copy) on every Add, and reading it back unboxes it. List<int> is generic and stores the ints inline in a backing int[] array — no boxing at all. For 100,000 elements this difference in allocations and GC pressure is significant.

Test yourself against the 3300-question Backend bank.

Start interview