The stack is a special area of memory that stores local variables and function call information. It works like a stack of plates: last in, first out. When a function is called, its variables are pushed onto the stack, and when it returns, they are popped off.
Click to reveal answer
beginner
What is the heap in memory management?
The heap is a large pool of memory used for dynamic allocation. Objects created with 'new' are stored here. Unlike the stack, the heap does not follow last-in, first-out order and requires manual or automatic management to free memory.
Click to reveal answer
intermediate
How does the stack differ from the heap in terms of size and speed?
The stack is smaller but faster because it uses a simple last-in, first-out structure. The heap is larger but slower because it manages memory dynamically and can become fragmented.
Click to reveal answer
intermediate
Why are value types stored on the stack and reference types on the heap in C#?
Value types (like int, struct) are stored on the stack because they have a fixed size and short lifetime. Reference types (like objects) are stored on the heap because they can be larger and have a lifetime that extends beyond the current function call.
Click to reveal answer
beginner
What happens if the stack runs out of space?
If the stack runs out of space, a stack overflow occurs, causing the program to crash or throw an error. This usually happens with very deep or infinite recursion.
Click to reveal answer
Where are local variables typically stored in C#?
AIn the CPU cache
BOn the heap
COn the stack
DIn the hard drive
✗ Incorrect
Local variables are stored on the stack because they have a short lifetime and fixed size.
Which memory area stores objects created with 'new' in C#?
AStack
BHeap
CRegister
DCache
✗ Incorrect
Objects created with 'new' are stored on the heap because they require dynamic memory allocation.
What is a key characteristic of the stack memory?
ALast-in, first-out order
BRandom access
CGarbage collected
DUnlimited size
✗ Incorrect
The stack uses last-in, first-out order to manage function calls and local variables.
What can cause a stack overflow error?
ASlow CPU
BToo many objects on the heap
CLarge arrays
DInfinite or very deep recursion
✗ Incorrect
Stack overflow happens when the stack runs out of space, often due to deep or infinite recursion.
Why is heap memory slower than stack memory?
ABecause it requires dynamic allocation and management
BBecause it is stored on the hard drive
CBecause it uses last-in, first-out
DBecause it is smaller
✗ Incorrect
Heap memory is slower because it needs to manage dynamic allocation and can become fragmented.
Explain the difference between stack and heap memory using a real-life analogy.
Think about how you organize things you use often versus things you store for later.
You got /4 concepts.
Describe what happens in memory when a C# method creates a local variable and an object with 'new'.
Consider where each type of data lives during the method execution.
You got /4 concepts.
Practice
(1/5)
1. In C#, where are value types like int and bool typically stored during program execution?
easy
A. On the heap, because they are objects that need dynamic memory
B. In static memory, shared across all instances
C. In the CPU cache only, not in stack or heap
D. On the stack, because they are stored directly and accessed quickly
Solution
Step 1: Understand value type storage
Value types like int and bool are stored on the stack because they hold data directly and have a short lifetime.
Step 2: Contrast with heap storage
Heap stores objects (reference types), not simple value types, which makes stack the correct place for these.
Final Answer:
On the stack, because they are stored directly and accessed quickly -> Option D
Quick Check:
Value types = stack [OK]
Hint: Value types go on stack; objects go on heap [OK]
Common Mistakes:
Thinking all data goes on heap
Confusing value types with reference types
Assuming stack is for objects only
2. Which of the following C# declarations correctly creates a reference type stored on the heap?
easy
A. string text = "hello";
B. double pi = 3.14;
C. bool flag = true;
D. int number = 5;
Solution
Step 1: Identify reference types
In C#, string is a reference type stored on the heap, while int, bool, and double are value types stored on the stack.
Step 2: Match declaration to heap storage
Only string text = "hello"; creates a reference type object on the heap.
Final Answer:
string text = "hello"; -> Option A
Quick Check:
Reference type = string [OK]
Hint: Strings are reference types stored on heap [OK]
Common Mistakes:
Assuming all primitives are reference types
Confusing string as value type
Ignoring heap for object storage
3. Consider the following C# code:
class Person { public string Name; } Person p1 = new Person(); p1.Name = "Alice"; Person p2 = p1; p2.Name = "Bob"; Console.WriteLine(p1.Name);
What will be the output and why?
medium
A. "Bob" because p1 and p2 reference the same object on the heap
B. Compilation error due to missing constructor
C. "Alice" because p1 and p2 are separate objects on the heap
D. "Alice" because strings are immutable
Solution
Step 1: Understand reference assignment
Both p1 and p2 point to the same Person object on the heap because p2 = p1; copies the reference.
Step 2: Trace the name change effect
Changing p2.Name to "Bob" changes the same object that p1 references, so p1.Name is also "Bob".
Final Answer:
"Bob" because p1 and p2 reference the same object on the heap -> Option A
Quick Check:
Reference copy = shared object [OK]
Hint: Reference copy means both variables point to same heap object [OK]
Common Mistakes:
Thinking p1 and p2 are independent copies
Confusing value and reference assignment
Ignoring that strings are immutable but object fields can change
4. Identify the error in this C# code snippet related to stack and heap usage: