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

Array as reference type behavior in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Array Reference Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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]);
A0
B1
CCompilation error
D10
Attempts:
2 left
💡 Hint
Arrays in C# are reference types, so changes via one reference affect the original array.
Predict Output
intermediate
2: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]);
A5
B1
C0
DRuntime error
Attempts:
2 left
💡 Hint
Reassigning 'copy' does not change 'original'.
🔧 Debug
advanced
2: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};
}
ABecause arrays are value types and passed by value
BBecause the new array is assigned but not returned
CBecause the parameter 'arr' is a copy of the reference, reassignment doesn't affect original
DBecause the method does not modify any element of the original array
Attempts:
2 left
💡 Hint
Think about how references are passed to methods in C#.
Predict Output
advanced
2: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;
}
A100
BCompilation error
C0
D4
Attempts:
2 left
💡 Hint
Modifying elements of the array affects the original array.
🧠 Conceptual
expert
3: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));
A10,20
B3,4
C1,2
D10,2
Attempts:
2 left
💡 Hint
Remember that 'y' is reassigned to a new array after 'x' is modified.