Difficulty: Easy
Correct Answer: 1, 2, 4
Explanation:
Introduction / Context:
Classes and structs differ in allocation, lifetime, and semantics. Understanding these distinctions helps you choose the right type for performance and design.
Given Data / Assumptions:
Concept / Approach:
1) Correct: class is reference type; struct is value type. 2) Correct: you can declare a struct variable without new (but you must assign all fields before use), or with new to run its constructor. 3) Incorrect: performance varies by scenario; no “always slower.” 4) Correct: a local struct value typically becomes eligible for reuse when its scope ends. 5) Incorrect: objects on the managed heap are reclaimed by GC based on reachability, not scope.
Step-by-Step Solution:
Verification / Alternative check:
Inspect IL or perform micro-benchmarks to observe stack vs heap behaviors and GC timing.
Why Other Options Are Wrong:
Common Pitfalls:
Assuming GC collects objects exactly at scope exit; it does not.
Final Answer:
1, 2, 4
Discussion & Comments