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

Jagged arrays in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Jagged Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
}
A1 2 \n3 4 5 \n
B1 2 3 4 5 \n
C3 4 5 \n1 2 \n
D1 2 3 \n4 5 \n
Attempts:
2 left
💡 Hint
Remember that each element of a jagged array can have different lengths.
Predict Output
intermediate
1: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]);
A20
B40
C30
DIndexOutOfRangeException
Attempts:
2 left
💡 Hint
Remember jagged arrays are arrays of arrays, so jagged[1] is the second inner array.
🔧 Debug
advanced
1: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;
ANo error, runs successfully
BIndexOutOfRangeException at runtime
CNullReferenceException at runtime
DCompile-time error: invalid array index
Attempts:
2 left
💡 Hint
Check the valid indices for jagged[0] which has length 3.
📝 Syntax
advanced
2: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?
Aint[] jagged[] = new int[2][] { new int[1], new int[2] };
Bint[,] jagged = new int[,] { {1}, {2, 3} };
Cint[][] jagged = new int[2] { {1}, {2, 3} };
Dint[][] jagged = new int[][] { new int[1], new int[2] };
Attempts:
2 left
💡 Hint
Jagged arrays use [][] notation and each inner array must be initialized separately.
🚀 Application
expert
2: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);
A21
B20
C19
D18
Attempts:
2 left
💡 Hint
Add all numbers: 1+2+3+4+5+6.