✍️ Practice (Hard)
1. Predict the Output (return edition)
Given the below program, what will be the output if i call Main?
static int[] Make()
{
int[] local = { 4, 5, 6 };
return local;
}
static void Main()
{
int[] r = Make();
r[1] = 0;
int[] s = Make();
Console.WriteLine($"{r[1]} {s[1]}");
}
👀Hint
local is popped off the stack when Make returns... but is the array? 👀
💡Solution
Each call to Make creates a brand new array on the heap. The local variable disappears when the frame is popped, but the array survives because Main still holds a reference to it.
SO r and s point to two different arrays.
0 5
2. Predict the Output (ref vs no ref)
Given the below program, what will be the output if i call Main?
static void Main()
{
int[] a = { 1, 2, 3 };
int[] b = a;
Change(a, ref b);
Console.WriteLine($"{a[0]} {b[0]}");
}
static void Change(int[] x, ref int[] y)
{
x[0] = 10;
y = new int[] { 20, 30 };
x[0]++;
y[0]++;
}
👀Hint
x is a copy of a's reference. y is actually b (thanks to ref)!
💡Solution
11 21
3. Draw the Memory
Given the program below, draw the stack and the heap at point (A) and again at point (B).
Label every variable, every array, and every arrow if Main is called.
static void Main()
{
int count = 2;
int[] data = { 7, 8 };
int[] copy = data;
Fill(data, count);
// (B)
}
static void Fill(int[] list, int value)
{
int[] other = new int[value];
list[0] = value;
// (A)
}
👀Hint
How many frames are on the stack at (A)? How many arrays are on the heap?
What happens to other's array once nothing points to it? 👀
💡Solution
At (A)
| Stack | Heap |
|---|---|
Fill frame: list → heap address 1, value = 2, other → heap address 2 | heap address 1 { 2, 8 } |
Main frame: count = 2, data → heap address 1, copy → heap address 1 | heap address 2 { 0, 0 } |
Two frames, two arrays. Three variables (data, copy, list) all point to the same array @ heap address 1.
At (B)
| Stack | Heap |
|---|---|
Main frame: count = 2, data → heap address 1, copy → heap address 1 | heap address 1 { 2, 8 } |
heap address 2 { 0, 0 } (nothing points here) |
The Fill frame was popped. Array @ heap address 2 is still on the heap, but nothing references it anymore, so the garbage collector is free to clean it up.
4. Fix the Error
This code is supposed to double every number, but it doesn't compile. Why? Fix it.
int[] nums = { 1, 2, 3 };
foreach (int n in nums)
{
n = n * 2;
}
👀Hint
a foreach loop is a read-only structure
💡Solution
C# won't let you assign to a foreach variable!
int[] nums = { 1, 2, 3 };
for (int i = 0; i < nums.Length; i++)
{
nums[i] = nums[i] * 2; // this is how you modify array contents!
}
5. Twins?
Given the below program, what will be the output?
int[] a = { 1, 2, 3 };
int[] b = { 1, 2, 3 };
int[] c = a;
Console.WriteLine(a == b);
Console.WriteLine(a == c);
Is there a better way to check if 2 arrays are the same? If so, write it.
👀Hint
== compares the addresses, not the items within the array 🔎
💡Solution
False
True
a and b are two separate arrays that happen to hold the same values, whereas a and c hold the same reference!
A better way to check the equality of 2 arrays would be by comparing each individual value, one at a time:
static bool AreEqual(int[] a, int[] b)
{
if (a.Length != b.Length)
{
return false;
}
for (int i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
{
return false;
}
}
return true;
}
6. Unique
Write Unique(int[] list) that returns a new array containing each value only once, in the order it first appears. The original array should not change.
int[] nums = { 4, 2, 4, 9, 2, 2, 7 };
int[] result = Unique(nums);
// result is { 4, 2, 9, 7 }
// nums is still { 4, 2, 4, 9, 2, 2, 7 }
👀Hint
Start with an empty result array. For each number, check if it's already in the result. If not, grow the result by one and add it.
💡Solution
static int[] Unique(int[] list)
{
int[] result = new int[0];
foreach (int n in list)
{
// by default, nothing has been seen
bool seen = false;
// if the num n is already in the result, don't add it again
foreach (int r in result)
{
if (r == n)
{
seen = true;
break;
}
}
// if its a new num n, that means we want to add it!
if (!seen)
{
Array.Resize(ref result, result.Length + 1); // grow bigger
result[result.Length - 1] = n;
}
}
return result;
}