A jagged array is an array of arrays, where each inner array can have a different length. It allows storing rows of different sizes, unlike a rectangular (multidimensional) array.
Click to reveal answer
beginner
How do you declare a jagged array of integers with 3 rows in C#?
You declare it like this: int[][] jaggedArray = new int[3][]; This creates an array with 3 elements, each of which can point to an integer array of any length.
Click to reveal answer
beginner
How do you initialize the second row of a jagged array to have 4 elements?
You assign a new array to the second row like this: jaggedArray[1] = new int[4]; This sets the second row to hold 4 integers.
Click to reveal answer
beginner
How do you access the third element of the first row in a jagged array?
You use two indexes: jaggedArray[0][2] The first index selects the row, the second index selects the element in that row.
Click to reveal answer
intermediate
What is the difference between a jagged array and a multidimensional array in C#?
A jagged array is an array of arrays where each inner array can have different lengths. A multidimensional array is a single block of memory with fixed size in each dimension. Jagged arrays offer more flexibility in row sizes.
Click to reveal answer
How do you declare a jagged array with 5 rows in C#?
Aint[][] arr = new int[5][];
Bint[] arr = new int[5,5];
Cint[] arr = new int[5];
Dint[][] arr = new int[][];
✗ Incorrect
Option A correctly declares a jagged array with 5 rows, each row can have different lengths.
Which syntax accesses the 2nd element of the 3rd row in a jagged array named 'jagged'?
Ajagged[2][1]
Bjagged[2,1]
Cjagged[3][2]
Djagged[1][2]
✗ Incorrect
Indexes start at 0, so the 3rd row is index 2 and the 2nd element is index 1.
Can jagged arrays have rows of different lengths?
AOnly if using special syntax.
BNo, all rows must have the same length.
COnly if declared as multidimensional arrays.
DYes, each row can have a different length.
✗ Incorrect
Jagged arrays are designed to allow rows of different lengths.
What happens if you try to access an element in a jagged array row that has not been initialized?
AIt returns zero by default.
BYou get a NullReferenceException.
CIt automatically initializes the row.
DIt throws a compile-time error.
✗ Incorrect
If the inner array is null (not initialized), accessing it causes a NullReferenceException.
Which of these is a correct way to initialize a jagged array with 2 rows, first with 3 elements and second with 2 elements?
Aint[] jagged = new int[2,3];
Bint[][] jagged = new int[2][3];
Cint[][] jagged = new int[2][]; jagged[0] = new int[3]; jagged[1] = new int[2];
Dint[][] jagged = { new int[3], new int[2] };
✗ Incorrect
Option C correctly declares the jagged array and initializes each row with different lengths.
Explain what a jagged array is and how it differs from a multidimensional array in C#.
Think about rows and their lengths.
You got /4 concepts.
Describe how to declare, initialize, and access elements in a jagged array in C#.
Remember jagged arrays are arrays of arrays.
You got /4 concepts.
Practice
(1/5)
1. What is a jagged array in C#?
easy
A. An array of arrays where inner arrays can have different lengths
B. A two-dimensional array with fixed rows and columns
C. An array that stores only integers
D. A single-dimensional array with fixed size
Solution
Step 1: Understand array types in C#
A jagged array is an array whose elements are arrays themselves, allowing different lengths for each inner array.
Step 2: Compare with other array types
Unlike two-dimensional arrays, jagged arrays do not require all inner arrays to have the same size.
Final Answer:
An array of arrays where inner arrays can have different lengths -> Option A
Quick Check:
Jagged array = array of arrays with varying lengths [OK]
Hint: Jagged arrays hold arrays inside arrays with different sizes [OK]
Common Mistakes:
Confusing jagged arrays with 2D arrays
Thinking all inner arrays must be same length
Assuming jagged arrays store only one data type
2. Which of the following is the correct way to declare a jagged array of integers in C#?
easy
A. int[][] jaggedArray = new int[3][];
B. int[] jaggedArray = new int[3,3];
C. int[] jaggedArray = new int[3][];
D. int jaggedArray[][] = new int[3][];
Solution
Step 1: Recall jagged array declaration syntax
Jagged arrays use double square brackets: int[][] to declare an array of arrays.
Step 2: Check each option
int[][] jaggedArray = new int[3][]; correctly declares a jagged array with 3 inner arrays uninitialized. int[] jaggedArray = new int[3,3]; is a 2D array, not jagged. int[] jaggedArray = new int[3][]; has wrong type on left side. int jaggedArray[][] = new int[3][]; has invalid syntax.
Final Answer:
int[][] jaggedArray = new int[3][]; -> Option A
Quick Check:
Use int[][] for jagged arrays [OK]
Hint: Use double brackets [][] to declare jagged arrays [OK]
Common Mistakes:
Using single brackets for jagged arrays
Confusing jagged arrays with 2D arrays
Incorrect syntax like int jaggedArray[][]
3. What will be the output of the following C# code?
int[][] jagged = new int[2][];
jagged[0] = new int[] {1, 2};
jagged[1] = new int[] {3, 4, 5};
Console.WriteLine(jagged[1][2]);
medium
A. 3
B. IndexOutOfRangeException
C. 2
D. 5
Solution
Step 1: Understand jagged array initialization
The jagged array has two inner arrays: jagged[0] = {1, 2} and jagged[1] = {3, 4, 5}.
Step 2: Access jagged[1][2]
jagged[1] is {3, 4, 5}, so index 2 is the third element, which is 5.
Final Answer:
5 -> Option D
Quick Check:
jagged[1][2] = 5 [OK]
Hint: Remember zero-based indexing for inner arrays [OK]
Common Mistakes:
Confusing indices and picking wrong element
Assuming all inner arrays have same length
Expecting runtime error for valid index
4. Identify the error in the following jagged array code:
int[][] jagged = new int[2][];
jagged[0] = new int[2] {1, 2};
Console.WriteLine(jagged[1][0]);
medium
A. Cannot use Console.WriteLine with jagged arrays
B. jagged[1] is not initialized before accessing
C. Inner arrays must have same length
D. Syntax error in jagged array declaration
Solution
Step 1: Check initialization of inner arrays
jagged[0] is initialized, but jagged[1] is not assigned any array.
Step 2: Accessing uninitialized inner array
Accessing jagged[1][0] causes a NullReferenceException because jagged[1] is null.
Final Answer:
jagged[1] is not initialized before accessing -> Option B
Quick Check:
Initialize all inner arrays before access [OK]
Hint: Always initialize each inner array before use [OK]
5. Given a jagged array int[][] data where each inner array represents scores of students in different subjects, how would you calculate the total sum of all scores?
hard
A. Use a single loop over data.Length and sum data[i][0] only
B. Sum only the first inner array elements
C. Use nested loops to iterate each inner array and sum all elements
D. Use jaggedArray.Sum() directly without loops
Solution
Step 1: Understand jagged array structure
Each inner array has varying length representing scores per student.
Step 2: Use nested loops to sum all elements
Outer loop iterates each inner array; inner loop sums elements inside each inner array.
Step 3: Avoid incorrect approaches
Summing only first inner array or first element misses data; jaggedArray.Sum() won't work directly as it's an array of arrays.
Final Answer:
Use nested loops to iterate each inner array and sum all elements -> Option C
Quick Check:
Nested loops sum all jagged array elements [OK]
Hint: Sum jagged arrays with nested loops over all inner arrays [OK]
Common Mistakes:
Summing only one inner array
Using single loop ignoring inner arrays
Expecting built-in sum on jagged array
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.