Skip to main content

✍️ 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;
}
}

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?

Explain why.

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.

Write the code using top-level statements. Read the score dynamically and display the correct medal.

Question 4​

Write a function that returns how many times a given character appears in a string.

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:

average±3×standard deviation\text{average} \pm 3 \times \text{standard deviation}

If any tablet falls outside this range, it is considered unstable.

demo

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:

average=∑i=1nxin=x1+x2+⋯+xnn\text{average} = \frac{\sum_{i=1}^{n} x_i}{n} = \frac{x_1 + x_2 + \dots + x_n}{n}

Implement Average(weights) which returns the average tablet weight

Part B: StandardDeviation(weights)​

The standard deviation measures how far the values typically are from the average:

standard deviation=∑i=1n(xi−average)2n=(x1−average)2+⋯+(xn−average)2n\text{standard deviation} = \sqrt{\frac{\sum_{i=1}^{n} \left(x_i - \text{average}\right)^2}{n}} = \sqrt{\frac{(x_1 - \text{average})^2 + \dots + (x_n - \text{average})^2}{n}}

Implement StandardDeviation(weights) which returns the standard deviation:

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 };

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.
safeunsafe

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