COSC 135B Exam 1 Practice
Tom Linton, September 26, 2000
  1. Short Answer
    1. Briefly describe each of the following terms:
      1. Java bytecode
      2. An algorithm
      3. GUI
      4. The Java Virtual Machine
      5. Compiler
      6. Syntax error
    2. Give a legal (and descriptive) name, as well as an illegal name for a variable that holds the name of the runner who just finished first in a race.
    3. Give below the declaration line for a variable of type double that holds an hourly wage, which is initialized to $5.65.
    4. Assuming the BreezyGUI package has been imported earlier in your file, give a legal declaration for a field that holds a bowling score (an integer).
    5. What is the difference between a class and an object?
    6. In Java, how do you say "If N is larger than or equal to 5, add 7 to N, otherwise set N equal to 3"?
    7. Give an example of a String literal.
    8. Given a double variable named costAfterRebate, use a call to the messageBox method to inform the user of their final cost (after deducting a rebate amount). Your message should have output similar to:
    Your cost after rebate is $14.21
      if the current value of costAfterRebate is 14.2123453. You should use Format.justify to obtain the correct precision for a monetary amount.
  1. Here is a program without any semicolons. Place semicolons where they belong:

    1. import java.awt.*
      import BreezyGUI.*

      public class ConvertTime extends GBFrame{

        // Build the GUI


        Label minutesLabel = addLabel("Give your time in minutes:",1,1,1,1)
        IntegerField minutesField = addIntegerField(0,1,2,1,1)
        Button convertButton = addButton("Convert",2,1,2,1)

        // declare some variables

        int hours = 0
        int minutes = 0

        public void buttonClicked(Button buttonObj){
           int newTime = minutesField.getNumber()
           if (newTime > 60)
              minutes = newTime % 60
           else
              minutes = newTime
           messageBox("The number of extra minutes is " + minutes)
        }

        public static void main (String[] args){
           Frame frm = new ConvertTime()
           frm.setSize(200,150)
           frm.setVisible(true)
        }

      }
  2. After adding all of the appropriate semicolons, the code above would compile and run, assuming the code is in a file with the correct name. What MUST the filename be?
  3. What is the output of the above code fragment if newTime is 134? What if newTime is 55?
  4. For each of the expressions below, insert parenthesis to show how Java evaluates the expression:
    1. v = 4 + 3 / 2;
    2. j = 4.2 + 5 - 7 % 4;
    3. y = 7 + 6 / 2.1;
    4. x = 8 * 5 % 3 - 1.0;
  5. Give the value of each of the variables (v, j, y, x) in the last question.
  6. At the end of a program where the output is just displayed, you need to print a String that indicates the number of nickels used (in your computation of "the answer"). The variable (of type int) named numberOfNickels contains the numeric value of the number of nickels you need in the answer. The quick and dirty solution would be to use the Java statement:
  7.    messageBox("You need " + numberOfNickels + " nickel(s).");
    but you have decided to impress your boss (me), by writing code that would print:
       you do not need any nickels.
       you need 1 nickel.
    or
       you need 2 nickels.

    (where, in the last case, the "2" might be replaced by any number equal to 2 or more), under the respective conditions that (numberOfNickels is zero), (numberOfNickels is one), or (numberOfNickels is bigger than or equal to two). Assuming that all values of numberOfNickels from zero to 10 are equally likely, what is the code you would use to replace the one "quick and dirty line of code"?

  8. Write a while loop that displays each integer from 1 to 6 on a separate line together with its square. You may use System.out.println and System.out.print, or a TextArea (assume the TextArea is already declared and its name is output).
  9. Assume that a user has entered a (double) value in the variable weightOfLetter, which equals the weight in ounces of a first class letter. Write code that will calculate and display the cost to mail this letter, assuming that the first ounce (or fraction of an ounce) costs 33 cents, each additional ounce costs 28 cents, but you cannot use first class postage if the letter weighs more than 36 ounces (in this case you must use 3rd class mail).
  10. The following code is neither intuitive nor commented, but nonetheless contains several features of the Java language that we have been exposed to. Trace the program and show or describe what it does as accurately as possible. (Hint: create a table showing the value of each variable as you go through the program.) Assume that Math.round takes a double as input and returns an int. Pay careful attention to the data types (double versus int) of the variables. If x is a double and equal to 2, you should write x = 2.0 to make it clear that x is the double value 2, not the integer value 2.
    int i = 1;

    while (i<10) {
       if (i / 2 == 2) {
          System.out.print("oops ");
       }
       else {
          System.out.println("i= " + i);
       }
       i = i + 3;
    }

        double x = 3.2;
    double y = 4.5;
    int j = 5;

    while (j>2) {
       x = x / 4;
       y = Math.round(x);
       System.out.println(y);
       j = j - 2;
    }