Difficulty: Easy
Correct Answer: Objects of smaller size are created on the heap.
Explanation:
Introduction / Context:
This question checks core understanding of how C#.NET treats classes (reference types) and where their instances are allocated. In .NET, class instances are created with new and live on the managed heap, where the garbage collector tracks them. Distinguishing heap vs. stack, and reference vs. value types, is essential for performance, lifetime, and semantics.
Given Data / Assumptions:
Concept / Approach:
When you write MyClass obj = new MyClass(); the variable obj (a reference) may be stored on the stack (if local), but the object data resides on the heap. This applies regardless of the object’s size. Value types (e.g., int, struct) can be stored inline (e.g., on the stack for locals), but classes do not behave that way. Therefore, any statement implying class objects live on the stack is incorrect.
Step-by-Step Solution:
Verification / Alternative check:
Inspect IL or use a memory profiler: class instances are heap objects and collected by the GC. Local references are small handles pointing to heap instances.
Why Other Options Are Wrong:
They either misclassify types (A), confuse stack with heap (B, D), or make a vague generalization (E) not tied to C# allocation rules.
Common Pitfalls:
Assuming object size changes allocation location; confusing variable (reference) location with object data location.
Final Answer:
Objects of smaller size are created on the heap.
Discussion & Comments