Skip to main content

Self Check

Self check questions are short review questions about the content of the chapter.

Short Answers

Note that bolded italized items are variable names

  1. What is the boolean expression that is true when number is between a and b inclusive?
  2. What is the boolean expression that is true when a number if positive?
  3. What is the boolean expression that is true if letter is either an exclamation mark (!) or question mark (?) ?
  4. What is the boolean expression that is true when the last digit of the number is a 5?
  5. Given:
    int a = 5;
    int b = 12;
    Evaluate the following to truthy or falsey:
    1. a == 12 || b == 5
    2. a == 5 && b == 12
    3. a == 5 || b == 12
    4. a - 5
    5. b
    6. !(a==12) && !(b==5)
    7. ((a==5) || (b==12)) && !(a==5 && b==12)

Walkthroughs

What is the exact output of the following program? Image of Walkthrough code


Programming Problems

Problem 1:

Write the following function:

double findPopCornPrice(char size, int hasButter, int hasSeaoning);

This function is passed the size of the popcorn (s, m or l), whether the user wants butter(0 for false, non-zero for true) and whether they want seasoning (0 for false, non-zero for true). returns the total price of popcorn from a local theater. The theater sells popcorn in two 3 sizes (s)mall - $5.00, (m)edium - $6.00 and (l)arge - $8.00. A customer can also add butter for 1.00 or purchase a bag of seasoning for $0.50

Problem 2:

Write the following function:

float TOURate(int dayOfWeek, int hour, int min);

Toronto Hydro's Time of Use (TOU) rate charges users based on when electricity is used. Depending on the day of the week and the time of day, a different rate is charged. This function passed a day of the week (0 for sunday, 1 for Monday, 2 for Tuesday, etc.), a time on the 24 hour clock (midnight is 0 hour 0 min) and returns the electrical rate for the given day/time. The following table describes the TOURates

Description and Day/TimeRate
Off Peak - Weekdays from 7:00 pm to 7:00 am and all day weekends7.6
Mid Peak - Weekdays from 7:01 am to 10:59am and 5:01pm to 6:59pm12.2
On Peak - Weekdays 11:00 am to 5:00 pm15.8

Debugging:

The following function is suppose to find the cost of shipping a package between two locations. The function is given the weight of the package in kilograms and the distance between the two locations. It is suppose to calculate the cost based on the following rules:

  • all packages have a base cost of $10.75
  • any package that has a weight of more than 5 kg gets charged an extra $2.10 per kg over 5kg
  • any package that has to be shipped more than 600 km gets charged $0.05 per 10 kilometer over 600km (rounding up. Thus 601 km is charged $0.05 more)
int ShippingCost(int Weight, int Distance){
int my_result = 10.75;
if(Weight > 5){
my_result + 2.10(Weight) = my_result
}
else if(Distance > 600){
my_rseult = 600 * 0.5
}
}
  1. In the above code identify the following:
    • all the syntax errors
    • all logical errors
    • all stylistic flaws that do not adhere to the style guide
  2. Making as few changes as possible to address all of the errors above, rewrite the function so that it functions according to spec