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:
Validate 1: true by definition in C#.Validate 2: true; both forms are legal for structs.Validate 3: false; creation cost depends on context and size.Validate 4: true; value leaves scope and its storage is reclaimed/overwritten.Validate 5: false; GC timing is independent of lexical scope.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