Bird
Raised Fist0
C Sharp (C#)programming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. In C#, what happens when you assign one array variable to another, like int[] a = {1, 2}; int[] b = a;?
easy
A. Both variables refer to the same array in memory.
B. A new copy of the array is created and assigned to b.
C. The array elements are copied individually to b.
D. The assignment causes a compile-time error.

Solution

  1. Step 1: Understand array assignment in C#

    Arrays are reference types, so assigning one array variable to another copies the reference, not the data.
  2. Step 2: Effect of assignment on variables

    Both variables point to the same array object in memory, so changes via one affect the other.
  3. Final Answer:

    Both variables refer to the same array in memory. -> Option A
  4. Quick Check:

    Array assignment copies reference, not data [OK]
Hint: Remember: arrays are references, assignment copies reference [OK]
Common Mistakes:
  • Thinking assignment copies array elements
  • Assuming a new array is created automatically
  • Confusing arrays with value types
2. Which of the following is the correct syntax to declare and assign an integer array with values 5, 10, 15 in C#?
easy
A. int arr = {5, 10, 15};
B. int[] arr = new int[3] {5, 10, 15};
C. int[] arr = new int {5, 10, 15};
D. int arr[] = new int[] {5, 10, 15};

Solution

  1. Step 1: Review array declaration syntax

    In C#, arrays are declared with the type followed by square brackets, and initialized with new and size or values.
  2. Step 2: Check each option

    int[] arr = new int[3] {5, 10, 15}; uses correct syntax with size and initializer. int arr = {5, 10, 15}; misses array brackets. int[] arr = new int {5, 10, 15}; misses the square brackets after int. int arr[] = new int[] {5, 10, 15}; uses C-style syntax which is invalid in C#.
  3. Final Answer:

    int[] arr = new int[3] {5, 10, 15}; -> Option B
  4. Quick Check:

    Correct array declaration uses new int[size] {values} [OK]
Hint: Use int[] arr = new int[size] {values}; for arrays [OK]
Common Mistakes:
  • Omitting square brackets in declaration
  • Using C-style array syntax
  • Missing new keyword with size
3. What is the output of the following C# code?
int[] a = {1, 2, 3};
int[] b = a;
b[0] = 10;
Console.WriteLine(a[0]);
medium
A. Compilation error
B. 1
C. 0
D. 10

Solution

  1. Step 1: Understand reference assignment

    Variable b is assigned the reference of array a, so both point to the same array.
  2. Step 2: Effect of modifying b[0]

    Changing b[0] to 10 changes the first element of the shared array.
  3. Step 3: Output of a[0]

    Since a and b reference the same array, a[0] is now 10.
  4. Final Answer:

    10 -> Option D
  5. Quick Check:

    Arrays share reference, so change reflects in both [OK]
Hint: Changing one reference changes the shared array [OK]
Common Mistakes:
  • Expecting original array to remain unchanged
  • Confusing value copy with reference copy
  • Assuming separate arrays after assignment
4. Consider this code snippet:
int[] x = {4, 5, 6};
int[] y = new int[3];
y = x;
y[1] = 20;
Console.WriteLine(x[1]);

What is the output and why?
medium
A. 5, because y was a new array and changes don't affect x.
B. 0, because y was initialized with zeros.
C. 20, because y references the same array as x after assignment.
D. Compilation error due to invalid assignment.

Solution

  1. Step 1: Analyze array assignment

    Initially, y points to a new array, but then y = x; makes y reference the same array as x.
  2. Step 2: Effect of modifying y[1]

    Changing y[1] to 20 modifies the shared array, so x[1] also becomes 20.
  3. Final Answer:

    20, because y references the same array as x after assignment. -> Option C
  4. Quick Check:

    Reference assignment means both variables share array [OK]
Hint: Assignment copies reference, so changes affect original array [OK]
Common Mistakes:
  • Thinking y remains a separate array after assignment
  • Assuming array elements are copied automatically
  • Confusing initialization with assignment
5. You want to create a copy of an integer array original so that modifying the copy does NOT affect the original. Which code correctly achieves this in C#?
hard
A. Both B and D
B. int[] copy = (int[])original.Clone();
C. int[] copy = original;
D. int[] copy = original.Select(x => x).ToArray();

Solution

  1. Step 1: Understand reference vs copy

    Assigning copy = original; copies the reference, so changes affect both.
  2. Step 2: Using Clone() and LINQ Select

    Clone() creates a shallow copy of the array, and Select with ToArray() creates a new array with copied elements.
  3. Step 3: Confirm both methods create independent arrays

    Both Clone() and Select(...).ToArray() produce new arrays, so modifying the copy won't affect the original.
  4. Final Answer:

    Both B and D -> Option A
  5. Quick Check:

    Use Clone() or LINQ to copy arrays [OK]
Hint: Use Clone() or LINQ to copy arrays, not assignment [OK]
Common Mistakes:
  • Using assignment instead of copying
  • Assuming Clone() returns reference
  • Not realizing LINQ creates new array
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard