✍️ Practice Test 3
Try every problem on paper first, then check the solution. Also redo all the array and function practice problems.
You should be comfortable with:
- Variables
- Counters vs. accumulators
- Loops (
for,foreach,while) - Function arguments and return values
- Arrays, and strings as arrays of char
- Using a loop variable as an array index
- Passing arrays to functions (arrays are passed by reference)
Question 1
What does this program print when Main() is called?
class Program
{
static int a;
static void Main(string[] args)
{
int b = 1;
int c = 2;
int d = 3;
a = 1;
Console.WriteLine("Starting Main");
b = MyFunc(d++, a, --c);
Console.WriteLine("Main: a=" + a + " b=" + b + " c=" + c + " d=" + d);
b = MyFunc(--c, b++, a);
Console.WriteLine("Main: a=" + a + " b=" + b + " c=" + c + " d=" + d);
}
static int MyFunc(int a, int b, int c)
{
a++;
--b;
c = a + b + c;
Console.WriteLine("MyFunc: a=" + a + " b=" + b + " c=" + c);
return a + c;
}
}
💡Solution
MyFunc: a=4 b=0 c=5
Main: a=1 b=9 c=1 d=4
MyFunc: a=1 b=8 c=10
Main: a=1 b=11 c=0 d=4
Question 2
Consider the following program:
static void Main(string[] args)
{
int[] scores = { 5, 10, 15 };
Modify(scores);
Console.WriteLine($"{scores[0]} {scores[1]} {scores[2]}");
}
static void Modify(int[] scores)
{
scores[0] = 100;
scores = new int[] { 7, 8, 9 };
scores[1] = 200;
}
What does this program print when Main() is called?
💡Solution
100 10 15
Explain why.
💡Solution
Modify receives a copy of the reference, so both variables point to the same array.
scores[0] = 100changes the shared array, and Main sees it TOO!scores = new int[] {...}points only the local variable at a new array. Main's variable still points to the original tho!scores[1] = 200changes the NEW array, which is thrown away when Modify ends
Question 3
A game awards medals based on a player's score:
- Score ≥ 90 → Gold Medal
- Score ≥ 75 → Silver Medal
- Score ≥ 50 → Bronze Medal
- Otherwise → No Medal
Would you use if-else or switch? Give one reason.
💡Solution
if-else
The rules are ranges which need comparison logic (>= 90), not exact values. A classic switch matches one value per case.
Write the code using top-level statements. Read the score dynamically and display the correct medal.
💡Solution
int score;
Console.Write("Enter the player's score: ");
score = int.Parse(Console.ReadLine());
if (score >= 90)
{
Console.WriteLine("Gold Medal");
}
else if (score >= 75)
{
Console.WriteLine("Silver Medal");
}
else if (score >= 50)
{
Console.WriteLine("Bronze Medal");
}
else
{
Console.WriteLine("No Medal");
}
Question 4
Write a function that returns how many times a given character appears in a string.
👀Hint
The method signature is as follows:
static int GetCharacterFrequency(char letter, string message)
{
// return smthing
}
💡Solution
static int GetCharacterFrequency(char letter, string message)
{
int total = 0;
foreach(char c in message)
{
if(c == letter)
{
total++;
}
}
return total;
}
Example usage:
string message = "wow hello there, welcome to my house";
Console.WriteLine(GetCharacterFrequency('w', message));
Question 5
In the pharmaceutical industry, control charts are used to make sure tablet production stays consistent. A batch is considered "under control" only if every tablet’s weight falls within this range:
If any tablet falls outside this range, it is considered unstable.

Your goal is to print the sample number of every out-of-control tablet. Start by writing two helper functions.
Part A: Average(weights)
The average is the sum of a set of values divided by how many values there are:
Implement Average(weights) which returns the average tablet weight
💡Solution
static double Average(double[] weights)
{
double total = 0;
foreach (double weight in weights)
{
total += weight;
}
return total / weights.Length;
}
Part B: StandardDeviation(weights)
The standard deviation measures how far the values typically are from the average:
Implement StandardDeviation(weights) which returns the standard deviation:
👀Hint
Read it from the inside out. Each step maps to one line of code:
| Step | Math | In code |
|---|---|---|
| 1. Find the average | Average(weights) | |
| 2. Find each value's distance from the average | weight - avg | |
| 3. Square it so negatives don't cancel positives | diff * diff | |
| 4. Add them all up | an accumulator in a loop | |
| 5. Divide by | / weights.Length | |
| 6. Take the square root | Math.Sqrt(...) |
💡Solution
static double StandardDeviation(double[] weights)
{
double avg = Average(weights);
double sumSquares = 0;
foreach (double weight in weights)
{
sumSquares += (weight - avg) * (weight - avg);
}
return Math.Sqrt(sumSquares / weights.Length);
}
Part C: Find the out-of-control tablets
Using your helpers and the arrays below, print the sample number of every tablet whose weight is outside the control limits.
int[] sampleNumbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
double[] weights = { 500, 501, 499, 500, 502, 498, 500, 501, 499, 500, 500, 530 };
👀Hint
sampleNumbers[i] and weights[i] describe the same tablet, so use a for loop, not a foreach.
💡Solution
int[] sampleNumbers = { 1, 2, 3, 4, 5, 6 };
double[] weights = { 498, 502, 500, 495, 505, 510 };
double avg = Average(weights);
double sDev = StandardDeviation(weights);
double lower = avg - SigmaLimit * sDev;
double upper = avg + SigmaLimit * sDev;
for (int i = 0; i < sampleNumbers.Length; i++)
{
if (weights[i] < lower || weights[i] > upper)
{
Console.WriteLine(sampleNumbers[i]);
}
}
Question 6
You are crossing an unstable Himalayan glacier. The hike is safe only if, from one hour to the next:
- the temperature never increases (staying the same is fine), and
- the temperature never drops by more than 5 °C.


Write IsSafeHike(double initialTemp, double[] temps), which returns true if the hike is safe and false otherwise.
Sample usage:
IsSafeHike(0, [-2, 3, 1, -1]) // false: temperature increases which means you could slip!!
IsSafeHike(0, [-1, -8, -10, -12]) // false: a 7°C drop is too steep
IsSafeHike(0, [-1, -3, -6, -7]) // true: always decreases and is never more than a 5°C drop
IsSafeHike(9, [-1, -3, -6, -7]) // false: a 10°C drop is too steep
💡Solution
static bool IsSafeHike(double initialTemp, double[] temps)
{
double currentTemp = initialTemp;
bool isSafe = true;
foreach (double t in temps)
{
if (t > currentTemp)
{
isSafe = false;
break;
}
if (currentTemp - t > 5)
{
isSafe = false;
break;
}
currentTemp = t;
}
return isSafe;
}