0
0
C Sharp (C#)programming~5 mins

Jagged arrays in C Sharp (C#) - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is a jagged array in C#?
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: <br>int[][] jaggedArray = new int[3][];<br>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:<br>jaggedArray[1] = new int[4];<br>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:<br>jaggedArray[0][2]<br>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.<br>A multidimensional array is a single block of memory with fixed size in each dimension.<br>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[][];
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]
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.
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.
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] };
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.