Difficulty: Easy
Correct Answer: 1, 4, 5
Explanation:
Introduction / Context:This question tests your understanding of C# array initialization syntax, array length, and indexing rules. You must recognize which declarations both compile and create a four-element array containing the specified values in the stated order without causing any runtime errors.
Given Data / Assumptions:
Concept / Approach:Legal C# forms include implicit-length initialization with braces, or explicit-length with a brace list, or allocate then assign each index within the bounds 0..Length-1. The expression new int{...} is invalid; you must write new int[]{...}. Writing to index 3 on an array created with new int[3] is an out-of-range error because valid indices are 0, 1, and 2.
Step-by-Step Solution:
(1) int[] a = {25, 30, 40, 5}; — Valid; compiler infers length 4. (2) new int[3] then write a[3] — Invalid at runtime (IndexOutOfRangeException). (3) new int{...} — Invalid syntax; missing [] after type. (4) new int[4]{25, 30, 40, 5}; — Valid explicit length and initializer. (5) new int[4] and then assign indices 0..3 — Valid.Verification / Alternative check:Compile each snippet separately. (1), (4), and (5) compile and run; (2) compiles but throws when executed; (3) fails to compile due to syntax.
Why Other Options Are Wrong:
Common Pitfalls:Forgetting that array indices are 0-based; confusing new int{...} with new int[]{...}; and mismatching declared length with the number of assigned elements.
Final Answer:1, 4, 5
Discussion & Comments