Homework #4

CSCI 1301 - Principles of Computer Programming I – Spring 2019

Quiz #4, on Thu., Feb. 28, will consist of questions taken or inspired from Parts I and II of this homework and

from the lab.

Part I — Questions

1. What is sequential processing?

2. What is a decision structure?

3. Decide if the following boolean expressions will evaluate to true or false:

• 3 > 2.0 && false

• (4 != 3) || false

• 'A' == 'b' && ! false

• (! false) == (true || 4 == 3)

4. What is the relational operator used to determine whenever two values are different?

5. What is a ﬂag?

6. Give three relational operators, and then two logical operators.

7. What would be displayed on the screen by the following code?

if (false)
{

Console.WriteLine("Hello!");

}
Console.WriteLine("Hi!");

8. Is there a simpler way to write the expression “over21 == true”, assuming that over21 is a Boolean variable?

9. Assume that x and y are two int variables that have already been initialized (i.e., declared and assigned), write
an if statement that assigns 10 to x if y is (strictly) greater than 5.

10. In C#, is there a difference between = and ==? Write a statement that use =.

11. Is the following statement correct, i.e., would it compile, assuming myFlag is a bool variable, and myAge is
an initialized int variable?

if ( myAge > 20 )
{

myFlag = true

};

12. Write an if statement that prints “Bonjour !” if the value of the char variable lang is 'f'.

13. For each of the following boolean expressions, decide if it will evaluate to true or false when the boolean
variables x, y and z are all set to true:

Thursday, February 21

spots.augusta.edu/caubert/pcp/

Page 1 of 4

Homework #4

CSCI 1301 - Principles of Computer Programming I – Spring 2019

• x || y && z

• !x || y && z

• !(x || y) && (z && y)

• (!x && x) || (!x || x)

Do the same when they are all set to false.

14. Write a boolean expression that evaluates to true if a variable x is between 3 (excluded) and 5 (included).
15. Write an if-else statement that assigns 0.1 to z if y is greater or equal than 0, and that assigns (cid:1)0.1 to z
otherwise.

16. What will be displayed on the screen by the following program?

int x = 3, y = 2, z = 4;
if (x > y) {z += y;}
if (x > z) {y -= 4;}
Console.WriteLine($"x is {x}, y is {y}, and z is {z}.");

17. What will be displayed on the screen by the following program?

int x = 3, y = 2, z = 4;
if (x >= z) {z += y;} else if (x != y) {z *= y;}
y -= 4;
Console.WriteLine($"x is {x}, y is {y}, and z is {z}.");

18. (We’ll use the 24-hour clock, sometimes called the “military time”.) Assuming that an int variable hour has
been initialized, write part of a program that would display on the screen “Good morning” if hours is less than
or equal to 12, and “Hello” otherwise.

19. Assuming that myString is a string variable, write a statement that print “Hello, Mélodie!” if the value of
myString is equal to Mélodie, and nothing otherwise.

20. What will be displayed on the screen by the following program?

int x = 3, y = 2, z = 4;
if (y >= z) {z += y;}
else if (x != y) { if (false) {z -= 3;} else {z += x;}}
Console.WriteLinef($"x is {x}, y is {y}, and z is {z}.");

21. Rewrite, if possible, the three following if-else-if statements as switch statements:

if (myLang == 'f') { Console.WriteLine("Vous parlez Français ?"); }
else if (myLang == 'e') { Console.WriteLine("Do you speak English?"); }
else if (myLang == 'd') { Console.WriteLine("Sprechen Sie Deutsch?"); }
else { Console.WriteLine("I don't know your language!"); }

if (myCity == "Augusta") { Console.WriteLine("I also live here!"); }
else if (myCity == "Paris" || myCity == "Boone")
{

Console.WriteLine("I used to live there!");

}
else

Thursday, February 21

spots.augusta.edu/caubert/pcp/

Page 2 of 4

1

2

3

4

1

2

3

4

5

6

Homework #4

CSCI 1301 - Principles of Computer Programming I – Spring 2019

7

8

9

1

2

3

{

}

Console.WriteLine("I never lived there.");

if (temp == 100.0) { Console.WriteLine("It's ready!"); }
else if (temp >= 90.0) { Console.WriteLine("Almost ready!"); }
else { Console.WriteLine("You have to wait."); }

If you think it is not possible or not feasible, explain why.

Part II – Problems

This time, the two exercises do not require a computer, and are here to craft on your problem-solving skills. Make
sure you feel ready before starting them, try to do them with a limited amount of time and without notes, and
check your answer using VS.

Problem 1

Write a program that asks the user to write a country name and stores the user’s input into a string variable. Then,
compare that string with "france": if it is equal, then print “Bienvenue en France !”. Then, compare that string
with "usa": if it is equal, then print “Welcome to the US!”. If the string is different from both "france" and "usa",
then print “Welcome to” followed by the name of the country the user typed in.

Can you think of two ways to implement this program, one using if-else-if statements, the other using

switch?

Problem 2

You want to write a small program for an on-line printing company.

Your program should ask the user to chose a format (10(cid:2) 15 centimeters, or 8(cid:2) 11 inches), ask if it is the ﬁrst

time the customer order through your company, and a number of copies.
Then, calculate the total cost of printing those pictures, knowing that
• Printing a 10(cid:2) 15 centimeters picture costs $0.20, printing a 8(cid:2) 11 inches picture costs $0.25,

• A new customer gets a $3 coupon if the order is more than $5,

• A 10% discount is given if more than 50 copies were ordered,

• The two previous offers can be cumulated.

Display at the screen a message starting by “Welcome!”, then a new line, then “We cherish our new customers”
if it is the ﬁrst time the user uses your company, “, so we’re giving you a $3 discount!” if the user is allowed to get
the coupon, then print the total and “You had a 10% discount!” if the user ordered more than 50 copies.

See Listings 1 and 2 for examples of execution, where the user input is underlined, and hitting carriage return

is represented by ↵.

m

Thursday, February 21

spots.augusta.edu/caubert/pcp/

Page 3 of 4

Homework #4

CSCI 1301 - Principles of Computer Programming I – Spring 2019

Enter 'c' for 10x15cm, anything else for 8x11in.
c ↵
Is this your first time here? Type 'y' for 'yes'.
y ↵
Enter a number of copies.
90 ↵
Welcome!
We cherish our new customers, so we are giving you a $3 discount!
Your total is $13,50. You had a 10% discount!

Listing 1: A First Example of Execution for Problem 2

Enter 'c' for 10x15cm, anything else for 8x11in.
p ↵
Is this your first time here? Type 'y' for 'yes'.
Not at all ↵
Enter a number of copies.
120 ↵
Your total is $27,00. You had a 10% discount!

Listing 2: A Second Example of Execution for Problem 2

Thursday, February 21

spots.augusta.edu/caubert/pcp/

Page 4 of 4

