Four short programs about combining and nesting conditions — Activities 1.15 and 1.16.
All four of these compile. All four run to the end without an error, a warning, or a crash. Every one of them is wrong.
That is not a coincidence — it is what this set is for. &&, ||, and ! take things that are already true or false and give back something that is also true or false. There is nothing left for C# to object to, so a logic error here produces a clean run and a confident wrong answer.
Three of these show you something wrong on the values you are given. One of them does not. On that one the printed output is exactly right and the program is still broken. You will have to change the values yourself to prove it, and saying which values expose it is part of the answer.
A 40-inch 10-year-old is told they may ride. The rule is supposed to be that a rider must be tall enough and old enough.
int height = 40;int age = 10;
if (height >= 48 || age >= 8){ Console.WriteLine("You may ride.");}else{ Console.WriteLine("Sorry, not this time.");}A member arrives on a Tuesday and is turned away by a message that says members are welcome on weekdays.
bool isWeekend = false;bool isMember = true;
if (isWeekend && isMember){ Console.WriteLine("Come in.");}else{ Console.WriteLine("Members only on weekdays.");}The message is correct and the condition does not match it. Say what the rule in the message actually is before you write the fix.
The tank is not full, so the warning should appear. Nothing prints at all.
bool isFull = false;
if (!isFull == false){ Console.WriteLine("Warning: tank is not full.");}Work out what !isFull is, then what comparing that to false gives you. Then write the condition the way you would say it out loud — there is a version of this line with no == in it at all.
Two dice, from Activity 1.16. With the values below it prints Doubles! and nothing else, which is exactly right.
int roll1 = 3;int roll2 = 3;
if (roll1 == roll2){ Console.WriteLine("Doubles!");}if (roll1 == 6){ Console.WriteLine("Double sixes -- total systems failure!");}Set roll1 to 6 and roll2 to 2, and run it again. Write down what it prints. Then say which line is in the wrong place and what it should be inside.
For each program, give:
Plus the two written answers:
== in itWork them out by reading first. Running the code tells you that something is wrong — on Bug 4 it will not even tell you that, and on the exam there is no computer.