Challenge - 5 Problems
Array Reference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of modifying array through reference
What is the output of the following C# code?
C Sharp (C#)
int[] arr = {1, 2, 3}; int[] refArr = arr; refArr[0] = 10; Console.WriteLine(arr[0]);
Attempts:
2 left
💡 Hint
Arrays in C# are reference types, so changes via one reference affect the original array.
✗ Incorrect
Both 'arr' and 'refArr' point to the same array in memory. Changing refArr[0] changes arr[0].
❓ Predict Output
intermediate2:00remaining
Effect of reassigning array reference
What is the output of this C# code snippet?
C Sharp (C#)
int[] original = {5, 6, 7}; int[] copy = original; copy = new int[] {1, 2, 3}; Console.WriteLine(original[0]);
Attempts:
2 left
💡 Hint
Reassigning 'copy' does not change 'original'.
✗ Incorrect
'copy' initially points to 'original' array, but after reassignment it points to a new array. 'original' remains unchanged.
🔧 Debug
advanced2:30remaining
Why does this array modification not affect original?
Consider this code:
int[] a = {1, 2, 3};
ModifyArray(a);
Console.WriteLine(a[0]);
void ModifyArray(int[] arr) {
arr = new int[] {9, 8, 7};
}
Why does the output remain 1?
C Sharp (C#)
int[] a = {1, 2, 3}; ModifyArray(a); Console.WriteLine(a[0]); void ModifyArray(int[] arr) { arr = new int[] {9, 8, 7}; }
Attempts:
2 left
💡 Hint
Think about how references are passed to methods in C#.
✗ Incorrect
The method parameter 'arr' is a copy of the reference. Reassigning it points it to a new array, but does not change the caller's reference.
❓ Predict Output
advanced2:00remaining
Output after modifying array element inside method
What is the output of this C# code?
C Sharp (C#)
int[] nums = {4, 5, 6}; ChangeFirst(nums); Console.WriteLine(nums[0]); void ChangeFirst(int[] arr) { arr[0] = 100; }
Attempts:
2 left
💡 Hint
Modifying elements of the array affects the original array.
✗ Incorrect
The method changes the first element of the array that both 'nums' and 'arr' reference.
🧠 Conceptual
expert3:00remaining
Understanding array reference behavior with multiple assignments
Given the code below, what is the final output?
C Sharp (C#)
int[] x = {1, 2}; int[] y = x; x[0] = 10; y = new int[] {3, 4}; x[1] = 20; Console.WriteLine(string.Join(",", y));
Attempts:
2 left
💡 Hint
Remember that 'y' is reassigned to a new array after 'x' is modified.
✗ Incorrect
'y' initially points to the same array as 'x'. After reassignment, 'y' points to a new array {3,4}. The Console.WriteLine prints the contents of 'y', which is now {3,4}.