Challenge - 5 Problems
Array Bounds Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of accessing array element within bounds
What is the output of this C# code snippet?
C Sharp (C#)
int[] numbers = {10, 20, 30}; Console.WriteLine(numbers[1]);
Attempts:
2 left
💡 Hint
Remember that array indexing in C# starts at 0.
✗ Incorrect
The array 'numbers' has elements 10 at index 0, 20 at index 1, and 30 at index 2. Accessing numbers[1] returns 20.
❓ Predict Output
intermediate2:00remaining
Output when accessing array element out of bounds
What happens when this C# code runs?
C Sharp (C#)
int[] arr = new int[3] {1, 2, 3}; Console.WriteLine(arr[3]);
Attempts:
2 left
💡 Hint
Check if the index 3 is valid for an array of length 3.
✗ Incorrect
Array indices go from 0 to length-1. Index 3 is outside the valid range, so an IndexOutOfRangeException is thrown.
🔧 Debug
advanced3:00remaining
Identify the cause of runtime error in array access
Why does this code throw an exception at runtime?
C Sharp (C#)
string[] fruits = {"apple", "banana", "cherry"};
for (int i = 0; i < fruits.Length; i++)
{
Console.WriteLine(fruits[i]);
}Attempts:
2 left
💡 Hint
Check the loop boundary condition carefully.
✗ Incorrect
The loop runs from i = 0 to i = fruits.Length inclusive. Since fruits.Length is 3, accessing fruits[3] causes an IndexOutOfRangeException.
🧠 Conceptual
advanced2:30remaining
Array bounds checking behavior in C#
Which statement best describes how C# handles array bounds checking?
Attempts:
2 left
💡 Hint
Think about safety features of C# compared to languages like C or C++.
✗ Incorrect
C# performs automatic bounds checking on arrays at runtime and throws IndexOutOfRangeException if an invalid index is accessed.
❓ Predict Output
expert3:00remaining
Output of multi-dimensional array access with invalid index
What is the output or result of running this C# code?
C Sharp (C#)
int[,] matrix = new int[2, 2] { {1, 2}, {3, 4} }; Console.WriteLine(matrix[1, 2]);
Attempts:
2 left
💡 Hint
Check the valid indices for a 2x2 array.
✗ Incorrect
The valid indices for a 2x2 array are 0 and 1 for both dimensions. Accessing matrix[1, 2] is out of bounds and throws an IndexOutOfRangeException.