Challenge - 5 Problems
Multi-dimensional Arrays Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a 2D array element access
What is the output of this C# code snippet?
C Sharp (C#)
int[,] matrix = { {1, 2, 3}, {4, 5, 6} }; Console.WriteLine(matrix[1, 2]);
Attempts:
2 left
💡 Hint
Remember that the first index is the row and the second is the column.
✗ Incorrect
The matrix has two rows and three columns. matrix[1, 2] accesses the element in the second row and third column, which is 6.
❓ Predict Output
intermediate2:00remaining
Length properties of a 3D array
Given the following 3D array, what is the output of the code below?
C Sharp (C#)
int[,,] cube = new int[2,3,4]; Console.WriteLine(cube.GetLength(1));
Attempts:
2 left
💡 Hint
GetLength(n) returns the size of the dimension n, starting from 0.
✗ Incorrect
The array has dimensions 2 (dim 0), 3 (dim 1), and 4 (dim 2). cube.GetLength(1) returns 3.
🔧 Debug
advanced2:00remaining
Identify the error in multi-dimensional array initialization
What error will this code produce when compiled or run?
C Sharp (C#)
int[,] grid = new int[2, 2] { {1, 2}, {3, 4}, {5, 6} };
Attempts:
2 left
💡 Hint
Check the declared size versus the number of initializer rows.
✗ Incorrect
The array is declared with size 2x2 but initialized with 3 rows, which causes a compile-time error.
❓ Predict Output
advanced2:00remaining
Output of nested loops over a 2D array
What will be printed by this code?
C Sharp (C#)
int[,] arr = { {1, 2}, {3, 4} }; for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { Console.Write(arr[i, j] + " "); } Console.WriteLine(); }
Attempts:
2 left
💡 Hint
The outer loop iterates rows, inner loop iterates columns.
✗ Incorrect
The code prints each row's elements separated by spaces, then a newline after each row.
🧠 Conceptual
expert3:00remaining
Memory layout of multi-dimensional arrays in C#
Which statement best describes how multi-dimensional arrays are stored in memory in C#?
Attempts:
2 left
💡 Hint
Think about how C# handles rectangular arrays compared to jagged arrays.
✗ Incorrect
C# rectangular multi-dimensional arrays are stored as a single contiguous block in row-major order, meaning rows are stored one after another.