Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
int[] a = [1, 2, 3];
int[] b = a;
b[0] = 10;
b = [7, 8];
Console.WriteLine(a[0]);
Console.WriteLine(b[0]);10 7
- Expecting
1 7:int[] b = acopies the reference, not the array, sob[0] = 10writes into the arrayaleads to. - Expecting
7 7:b = [7, 8]puts a new reference intobonly, andastill leads to the old array with10in it.
- What would the program print if the line
b = [7, 8];were removed? - How would you make
ban independent copy ofabefore changing it?