C#.NET — Jagged array bounds: evaluate GetUpperBound on the second inner array. Code: int[][] a = new int[2][]; a[0] = new int[4]{6, 1, 4, 3}; a[1] = new int[3]{9, 2, 7}; Console.WriteLine(a[1].GetUpperBound(0));
Correct Answer: 2
Introduction / Context:This tests your knowledge of array indexing and the GetUpperBound method. In a 1-D array of length N, valid indices are 0..N-1, and GetUpperBound(0) returns N-1.
Given Data / Assumptions:
- a[1] is a 1-D int[] with 3 elements: {9, 2, 7}.
- We ask for the upper bound of dimension 0.
Concept / Approach:For any 1-D array arr of length L, arr.GetUpperBound(0) equals L - 1. Here L = 3, so the highest valid index is 2. Values like 3, 4, 7, or 9 are element values or lengths, not the upper index bound.
Step-by-Step Solution:
Length(a[1]) = 3. Upper bound = 3 - 1 = 2. Console prints 2.Verification / Alternative check:Printing a[1].Length yields 3; confirming upper bound must be 2.
Why Other Options Are Wrong:They confuse value, length, or an off-by-one index with the correct upper bound.
Common Pitfalls:Off-by-one errors when thinking about 0-based indexing.
Final Answer:2