Difficulty: Easy
Correct Answer: foreach (int j in i)
Explanation:
Introduction / Context:
Jagged arrays are arrays of arrays. To iterate every value, first enumerate each inner array, then enumerate its elements.
Given Data / Assumptions:
a
is int[][]
with rows of different lengths.i
.
Concept / Approach:
The idiomatic nested foreach is: foreach (int[] i in a) { foreach (int j in i) { ... } }
. This naturally handles different row lengths without index math.
Step-by-Step Solution:
i
is an int[]
).Inner loop: iterate values in i
→ foreach (int j in i)
.Print each j
followed by a space, then write a newline per row.
Verification / Alternative check:
Run the code; it prints 6 1 4 3
on the first line and 9 2 7
on the second, matching the jagged structure.
Why Other Options Are Wrong:
a.Length
is a scalar; you cannot foreach over an int
.
Common Pitfalls:
Confusing rectangular array APIs (like GetUpperBound
) with jagged arrays, which are just arrays whose elements are arrays.
Final Answer:
foreach (int j in i)
Discussion & Comments