CSCI 1301 – Quiz

Clément Aubert

October 27, 2020

Question 1

For each of the following boolean expressions, decide if it will evaluate to true or false:

  1. true || 3 != 4
  2. 4 >= 4 && ! false
  3. ('c' == 'd') && true
  4. (true && 4 >= 3) == false

This problem is left as an exercise. If you want to check your answers, you can use e.g

Console.WriteLine(true || 3 != 4);

Question 2

Section A

Write an if-else statement that displays “It’s free for you!” if an int variable age is between 0 and 18.

if (age >= 0 && age <= 18)Console.WriteLine("It's free for you!");

Section B

Assuming a name string was declared and initialized with a value given by the user, write an if statement that displays “I have the same name!” if name contains your first name.

if (name == "Clément")Console.WriteLine("We have the same name!");

Question 3

Section A

Assume initialized an int variables called graduationYear and a string variable called graduationSemester. Write a series of statements that would display “I will graduate at the same time!” if graduationYear is 2023 and graduationSemester is Fall, “I love this season.” if graduationSemester is Winter, “That’s in a long time!” if graduationYear is greater than 2025, and “I hope you’ll have in person ceremony!” otherwise. Your program should display exactly one message.

if(graduationYear == 2023 && graduationSemester == "Fall"){
    Console.WriteLine("I will graduate at the same time!");
}
else if (graduationSemester == "Winter"){
    Console.WriteLine("I love this season!");
}
else if (graduationYear > 2025){
    Console.WriteLine("That's in a long time!");
}
else{
    Console.WriteLine("I hope you'll have in person ceremony!");
}

Section B

Assume initialized an int variables called courseNumber and a string variable called courseCode. Write a series of statements that would display “I’m taking this class!” if courseNumber is 1301 and courseCode is CSCI, “That’s my major!” if courseCode is CSCI, “Is that an elective?” if courseCode is greater than 3000, and “Is it a good class?” otherwise. Your program should display exactly one message.

You can take inspiration from the answer posted for Section A to figure this one out.

Question 4

Give an example of an if statement that could not be rewritten as a switch.

Basically anything using an operator that is not == could work. For instance,

if (temperature > 10){…}

Or anything involving multiple variables could work too (even if it could be mimicked by nested switch in some cases):

if (age > 18 && usCit){…}
if (age == legalLimit){…}