Skip to main content

✍️ Practice Test 1

Question 1​

The code below contains syntax and logic errors. Find every error and correct them.

int daysPassedDeadline = 2;
int labCompleted = true;

if (labCompleted && daysPassedDeadline <= 3);
Console.WriteLine("Congrats your assignment has been completed!");
Console.WriteLine("Poggie will grade that in 5-7 business days");

Console.WriteLine("Next lab will be out in October!");

Question 2​

The C# code below runs from top to bottom, one line at a time. After each line runs, write down the current value of every variable (a, b, c, d) in the blanks on that line.

int a, b, c, d;

a = 3; // a=___________ b=___________ c=___________ d=___________
b = 2; // a=___________ b=___________ c=___________ d=___________
c = a + b; // a=___________ b=___________ c=___________ d=___________
d = c + b; // a=___________ b=___________ c=___________ d=___________
b = b + a; // a=___________ b=___________ c=___________ d=___________
a = a + b - c; // a=___________ b=___________ c=___________ d=___________
c = c + a; // a=___________ b=___________ c=___________ d=___________
c = c - 1; // a=___________ b=___________ c=___________ d=___________
a = a + b; // a=___________ b=___________ c=___________ d=___________
d = a / c; // a=___________ b=___________ c=___________ d=___________

Console.WriteLine("a=" + a + " b=" + b + " c=" + c + " d=" + d);

Write the exact text that the Console.WriteLine statement displays on the screen.

Question 3​

This program should print what to wear based on the temperature (°C) and whether it's raining:

We should wear a jacket if it's cold or raining, otherwise we should wear a space suit at extreme temperatures. In Canada, -63°C and 49.6°C are the current recorded extremes.

double temp = -270;
bool isRaining = false;

if (temp < 0 || isRaining)
{
Console.WriteLine("🧥");
}
else if (temp < -63 || temp > 49.6)
{
Console.WriteLine("🧑‍🚀");
}

The code compiles and runs, but suggests we only need a jacket if we are experiencing the baseline temperature of outer space.

Find the logic error, correct the code, and explain why it happens.

Question 4​

p and q are bool variables. Is the following expression true for every possible combination of values of p and q?

bool result = (!p || (q && !p)) == (q || !(q && p));

Justify your answer in the table below showing the value of the result for each combination of p and q.

pqLeft SideRight SideResult
truetrue
truefalse
falsetrue
falsefalse

Question 5​

Jimmy the intern created a program to calculate the total number of hours he needs to spend on homework each week. Each variable stores the homework hours earlier in the code:

Console.WriteLine($"Wow i need to study {p1 + prodTools + techSup + eng + french}");

Before the add/drop deadline, Jimmy decides to drop one of his classes. Explain why Jimmy's current program is difficult to maintain now that his list of classes has changed.

Question 6​

The program prints "We can adopt this new (delightful) pet fish ♡" only if we have enough money, the fish is over one year old, and the fish will have friends at home.

double cash = 70.75;
double fishAge = 2;
int fishAtHome = 2;

if (cash > 60.99)
{
if (fishAge > 1)
{
if (fishAtHome > 0)
{
Console.WriteLine("We can adopt this new (delightful) pet fish ♡");
}
}
}

The program works, but its nested if statements and magic numbers make it harder to maintain. Rewrite it using a single if statement and constants where appropriate.

Question 7​

Welcome to the JAC Computer Store!

ItemCost
Computer$1500
Keyboard$50
Monitor$200

At our store we have a few rules:

  • Each computer must be sold with a keyboard. Orders that don't include enough keyboards should be updated to include enough.
  • When a computer and a monitor are bought together, the customer gets a $50 discount on their purchase.

Design a program that asks the user how many computers, keyboards, and monitors they want, applies the rules, and displays the total cost.

Your design must be pseudocode or a flowchart:

Then, turn your design into a short C# program: