✍️ 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!");
💡Solution
const int GracePeriod = 3;
int daysPassedDeadline = 2;
bool labCompleted = true;
if (labCompleted && daysPassedDeadline <= GracePeriod)
{
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!");
The errors were:
labCompletedis assignedtrue, so it must be abool, not anint.- The
;after theifcondition ends the statement early, so nothing is actually controlled by the condition. - Without curly braces, only the first line after the
ifbelongs to it. Both congratulation messages need to be wrapped in{ }.
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.
💡Solution
The variables change as follows:
a = 3; // a=3 b=– c=– d=–
b = 2; // a=3 b=2 c=– d=–
c = a + b; // a=3 b=2 c=5 d=–
d = c + b; // a=3 b=2 c=5 d=7
b = b + a; // a=3 b=5 c=5 d=7
a = a + b - c; // a=3 b=5 c=5 d=7
c = c + a; // a=3 b=5 c=8 d=7
c = c - 1; // a=3 b=5 c=7 d=7
a = a + b; // a=8 b=5 c=7 d=7
d = a / c; // a=8 b=5 c=7 d=1
The output is:
a=8 b=5 c=7 d=1
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.
💡Solution
double temp = -270;
bool isRaining = false;
if ((temp >= -63 && temp < 0) || isRaining)
{
Console.WriteLine("🧥");
}
else if (temp < -63 || temp > 49.6)
{
Console.WriteLine("🧑🚀");
}
The first condition temp < 0 is true for every temperature below zero, including the extreme ones like -270°C. Since an if / else if chain only runs the first branch that is true, the space suit branch is never reached for cold extremes.
The fix requires that the temperature be between the extreme temperatures for the jacket to be worn.
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.
| p | q | Left Side | Right Side | Result |
|---|---|---|---|---|
| true | true | |||
| true | false | |||
| false | true | |||
| false | false |
💡Solution
No, the expression is not true for every combination.
| p | q | Left: !p || (q && !p) | Right: q || !(q && p) | Result |
|---|---|---|---|---|
| true | true | false | true | false |
| true | false | false | true | false |
| false | true | true | true | true |
| false | false | true | true | true |
Left side sample calculation:
!p || (q && !p)
= !true || (true && !true)
= false || (true && false)
= false || false
= false
Right side sample calculation:
q || !(q && p)
= true || !(true && true)
= true || !true
= true || false
= true
Result: false == true → false
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.
💡Solution
The total is hardcoded as a list of specific variable names added together. When Jimmy drops a class, he has to find every place that variable is used (like this sum) and edit it by hand. If he forgets one, the total will still include hours for a class he no longer takes, or the program won't compile once he deletes the variable.
The more classes he adds or drops, the more places need changing, which makes bugs more likely.
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.
💡Solution
// constants always @ top
const double FishPrice = 60.99;
const double MinFishAge = 1;
const int MinFishFriends = 1;
// variables in da center
double cash = 70.75;
double fishAge = 2;
int fishAtHome = 2;
// update conditions
if (cash > FishPrice && fishAge > MinFishAge && fishAtHome >= MinFishFriends)
{
Console.WriteLine("We can adopt this new (delightful) pet fish ♡");
}
Question 7
Welcome to the JAC Computer Store!
| Item | Cost |
|---|---|
| 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:
💡Solution
Then, turn your design into a short C# program:
💡Solution
const int ComputerPrice = 1500;
const int KeyboardPrice = 50;
const int MonitorPrice = 200;
const int Discount = 50;
Console.WriteLine("Welcome to the JAC Computer Store!");
Console.Write("How many computers? ");
int computers = int.Parse(Console.ReadLine());
Console.Write("How many keyboards? ");
int keyboards = int.Parse(Console.ReadLine());
Console.Write("How many monitors? ");
int monitors = int.Parse(Console.ReadLine());
// Rule 1: each computer needs a keyboard
if (keyboards < computers)
{
keyboards = computers;
}
int total = computers * ComputerPrice
+ keyboards * KeyboardPrice
+ monitors * MonitorPrice;
// Rule 2: computer + monitor discount
if (computers > 0 && monitors > 0)
{
total -= Discount;
Console.WriteLine($"Discount applied: -${Discount}");
}
Console.WriteLine($"Total cost: ${total}");