Difficulty: Easy
Correct Answer: int[] a = new int[5]; a = new int[10];
Explanation:
Introduction / Context:
Arrays in C# are fixed-size. You cannot change the Length of an existing array instance. To “resize”, you create a new array and make the reference point to it, optionally copying old elements. This question asks which snippet reflects that rule (without requiring data preservation).
Given Data / Assumptions:
Concept / Approach:
Because Length is read-only and the array object is immutable in size, you must instantiate a new int[10] and assign it to the same variable. Redeclaring the variable in the same scope is illegal. Methods like GetUpperBound are for querying bounds, not resizing.
Step-by-Step Solution:
Verification / Alternative check:
To preserve data, use Array.Resize(ref a, 10); which internally creates a new array and copies elements.
Why Other Options Are Wrong:
Common Pitfalls:
Trying to mutate Length, or shadowing the variable in the same scope rather than assigning a new instance.
Final Answer:
int[] a = new int[5]; a = new int[10];
Discussion & Comments