Mid Term 2 Review

Exam Policies and Rules

Make sure you've read over Assignment & Exam Policies!

For Virtual Classes

For Face-to-Face Classes

For ALL Classes

Plan ahead!! Make sure you don't sleep in, as that is not a valid reason for missing a test. Make sure you leave for school earlier than normal in case you encounter transportation problems on the way (i.e traffic jam, broken-down bus, etc). If you drive, make sure you have a bus schedule - if your car won't work when you leave for school, you'll have the time and resources to take the bus, instead.

Remember, this exam is worth 25% of your final mark.

Exam Format

The exam will include the following types of questions:

Summary of Topics

The exam will cover everything you've learned to date, although the focus will be on the new material covered in weeks 5 to 8. This includes:

This may not be a complete list. Make sure you've read the required textbook sections!

Selection Structure and Conditions

Iteration

Other

Practice Questions

Study Hints

[ SOLUTIONS ]

1. Find the errors in each of these code segments (each segment is from a different program):

a.

int c = keysIn.nextInt();
if (c => 7); 
   System.out.println("c is less than 7");

b.

int product;
int c = keysIn.nextInt();
While (c <= 5 )
   product *= c;
   c++;

c.

public class Something {
   public static void main(String[] args) {
      int number = 5;
      do
      {
         double cube = Math.pow(number, 2);
      while (number > 0) 
      }
   }
}

2. Find the Output:

a.

int i=5, j=10;
if (i < 5)
   System.out.println("foo");
   if (j >= 5)
      System.out.println("bar");
else
   System.out.println("yee");

b.

int counter = 0, sum = 0;
do {
   System.out.println(counter++);
   sum += counter;
} while (counter <=5);
System.out.println(sum + ", " + counter);

c.

int counter = 1;
while (counter <=5) {
   System.out.print(counter + ": ");
   if (counter % 2 == 0)
      System.out.println("even");
   else
      System.out.println("odd");
}

d. (user enters "hello" and 4)

System.out.println("Enter a name.");
String name = keysIn.next();
keysIn.nextLine();
System.out.println("Enter a number.");
int num = keysIn.nextInt();

if (num % 2 == 0) {
   int counter = 0;
   while (counter <= num) {
      System.out.println(counter + " " + name);
      counter += 2;
   }
} else {
   int counter = num;
   do {
      System.out.println(name);
      counter--;
   } while (counter >= 1);
}

e. do question d again, where user enters "hello" and 3

f.

for (int row=1; row<=6; row++)
{
   int space;
   for (space=6; space>row; space--) {
      System.out.print(" ");
   }
			
   for (int col=space; col>=1; col--) {
      System.out.print(col);
   }
   System.out.println();
}

Coding Questions

1. A company calculates the cost of shipping packages by the weight of the package. Packages have a tiered cost based on weight:

Up to 2 Kg: $1.15 per Kg
Up to 5 Kg: $1.35 per Kg
Up to 10 Kg: $1.75 per Kg
10 Kg and over: $2.00 per Kg

Write a program that prompts the user for the user to enter the package weight. Determine the cost per Kilogram based on the weight of the package. Then calculate and display the total shipping cost.

2. Write a program for a mall kiosk that handles ticket sales for a holiday charity event. The person manning the kiosk sells tickets to people passing by at $19.99 per ticket. If a person purchases 10 or more tickets, they get a 5% discount on the ticket price. Instead of purchasing tickets, people can make a donation of any amount they like. Your program should ask the user if tickets are being purchased (Y/N). If they say yes, the number of tickets should be retrieved, and the discount should be included if appropriate. If the person is not purchasing tickets, the donation amount should be retrieved instead. After the values have been recorded, an "invoice" displaying the amount to collect should be displayed, formatted, on the screen.

3. Write a program using a for-loop that displays a conversion table for pounds and grams (one pound = 453.6 grams). The start and end value should be entered by the user. Example below:

Enter Start Value: 1
Enter End Value: 20

Conversion Table:  LBS to GRAMS
LBS             GRAMS
----            ------
 1.0             453.6
 2.0             907.2
 3.0            1360.8
 4.0            1814.4
 5.0            2268.0
 6.0            2721.6
 7.0            3175.2
 8.0            3628.8
 9.0            4082.4
10.0            4536.0
11.0            4989.6
12.0            5443.2
13.0            5896.8
14.0            6350.4
15.0            6804.0
16.0            7257.6
17.0            7711.2
18.0            8164.8
19.0            8618.4
20.0            9072.0

Extra: If the user enters a start value that's larger than the end value, swap the start and end values.

Additionally, try the following programming questions from the end of chapter 3 and chapter 5: