Challenge - 5 Problems
Jagged Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Jagged Array Initialization
What is the output of the following C# code that initializes and prints a jagged array?
C Sharp (C#)
int[][] jagged = new int[2][]; jagged[0] = new int[] {1, 2}; jagged[1] = new int[] {3, 4, 5}; foreach (var arr in jagged) { foreach (var num in arr) { Console.Write(num + " "); } Console.WriteLine(); }
Attempts:
2 left
💡 Hint
Remember that each element of a jagged array can have different lengths.
✗ Incorrect
The jagged array has two inner arrays: the first with 2 elements (1,2) and the second with 3 elements (3,4,5). The nested loops print each inner array on its own line.
❓ Predict Output
intermediate1:30remaining
Accessing Jagged Array Elements
What value is printed by this C# code snippet?
C Sharp (C#)
int[][] jagged = new int[3][]; jagged[0] = new int[] {10}; jagged[1] = new int[] {20, 30}; jagged[2] = new int[] {40, 50, 60}; Console.WriteLine(jagged[1][1]);
Attempts:
2 left
💡 Hint
Remember jagged arrays are arrays of arrays, so jagged[1] is the second inner array.
✗ Incorrect
jagged[1] is the second inner array {20, 30}. Accessing index 1 of this array gives 30.
🔧 Debug
advanced1:30remaining
Identify the Error in Jagged Array Assignment
What error will this C# code produce when compiled or run?
C Sharp (C#)
int[][] jagged = new int[2][]; jagged[0] = new int[3]; jagged[1] = new int[2]; jagged[0][3] = 5;
Attempts:
2 left
💡 Hint
Check the valid indices for jagged[0] which has length 3.
✗ Incorrect
jagged[0] has length 3, so valid indices are 0,1,2. Accessing index 3 causes IndexOutOfRangeException at runtime.
📝 Syntax
advanced2:00remaining
Correct Syntax for Jagged Array Declaration
Which option correctly declares and initializes a jagged array of integers with 2 inner arrays, the first with 1 element and the second with 2 elements?
Attempts:
2 left
💡 Hint
Jagged arrays use [][] notation and each inner array must be initialized separately.
✗ Incorrect
Option D correctly declares a jagged array with two inner arrays of sizes 1 and 2. Option D uses a rectangular array syntax. Option D has invalid syntax mixing sizes and initializers. Option D has invalid declaration syntax.
🚀 Application
expert2:30remaining
Sum of All Elements in a Jagged Array
Given the jagged array below, what is the value of the variable 'total' after running the code?
C Sharp (C#)
int[][] jagged = new int[][] { new int[] {1, 2, 3}, new int[] {4, 5}, new int[] {6} }; int total = 0; foreach (var inner in jagged) { foreach (var num in inner) { total += num; } } Console.WriteLine(total);
Attempts:
2 left
💡 Hint
Add all numbers: 1+2+3+4+5+6.
✗ Incorrect
The sum is 1+2+3=6, plus 4+5=9, plus 6 = 6+9+6=21.