COSC 135 A Exam 1 Practice Problems , Tom Linton Spring 2003
The problems on this practice exam are similar in difficulty level to those that will appear on the first exam. This is NOT intended to be a laundry list of the things you should study for exam 1. I have attempted to include most of the important concepts from chapters 0 to 3 on this practice sheet, but certain topics not included here may show up on the exam. Note, we skipped sections 2.6 (Applets) and 3.8 (Numerical Representation), they will NOT be on the exam.
  1. What are comments used for in a Java program? Give two ways to include comments in a source code file.
  2. Write a single Java statement that assigns to the variable r, the remainder when the variable total is divided by 25 (assume that r and total are already declared and are of type int).
  3. State the order of evaluation of the operators in each of the following Java statements and show the value of x after each statement is performed. Assume that n is a properly declared variable of type int.
    1. n = 7 + 3 * 6 / 5 - 1;     b. n = 2 % 2 + 2 * 2 - 2 / 2;
  4. Write Java statements (or one statement) to declare and create a variable of the class MainWindow.
  5. Assume that x is a double variable that has been properly declared. What value is strored in x by the commands below (be sure to use things like 5.0 instead of 5 here)?
    1. x = (double) 10 / 4 + 5.0;      c) x = Math.ceil(-14.3) / 10;
    2. x = (double) (16 / 10);         d) x = Math.pow(5, 2);
  6. Give brief definitions or explanations of the following terms:
    1. CPU                         d. high level language
    2. bytecode                   e. compiler
    3. bit                              f. double
  7. What is the main difference between an object of some class and a primitive data type (variable)?
  8. Which of the following are valid Java identifiers (circle them)? Which valid identifiers below (cross them out) violate standard programming conventions for naming objects?
  9. myTurtle 3dGlasses noodleMania4
    my Turtle x8 MrMagoo
    SNapPY ok?samIam double
  10. You have a Circle object named wilma and want to move wilma 50 pixels to the right. Give the syntax for invoking wilma's moveHorizontal method with the parameter (input) of 50.
  11. Show the change in variables as the statements below are executed:

  12.     int num = 398, total;
        total = num % 10;
        num = num / 10;
        total = total + num % 10;
        num = num / 10;
        total = total + num;
  13. If you have a MessageBox named talky (already declared and instantiated), write a Java statement that prints the value of the int variable weight (already declared and holding a value) followed by the String ounces (with a space in between them).
  14. As accurately as possible, write the source code for a program named Greeting that asks the user for their name and their age and then prints (using an OutputBox) a message similar to:

  15.        Hello there Tom,
           in ten years you will be 51 years old.
    The bold expressions should change with the user's input. In the example above, the user entered Tom for their name and 41 for their age.


  16. If you created the program in the last question on the computer, what must be the full name (filename and extension) of the file?
  17. Convert (a) and (b) below from binary to decimal (base 10), and (c) and (d) from decimal to binary (base 2).
    1. 1011
    2. 111000
    3. 32
    4. 50
  18. Consider the program below:
    1. /* A silly program for an exam
      * by Tom Linton, working alone
      * COSC 135 A, February 12, 2003 */
      import javabook.*;
      public class Silly
      {
          public static void main(String args[])
          {
          MainWindow glassy = new MainWindow();
          OutputBox gabby = new OutputBox(glassy);
          InputBox asky = new InputBox(glassy);
          Clock ticky = new Clock();
          glassy.show();
          gabby.show();
          int score1, score2, score3;
          score1 = score2 = score3 = 0;
          double avg;
          final double LOWESTA = 90.0;
          gabby.printLine("This may take a while, please be patient!");
          ticky.pause(15);
          score1 = asky.getInteger("Enter score on exam 1");
          score2 = asky.getInteger("Enter score on exam 2");
          score3 = asky.getInteger("Enter score on exam 3");
          avg = (score1 + score2 + score3) / 3.0;
          gabby.printLine("Your average for the three exams is " + avg);
          gabby.skipLine(2);
          gabby.printLine("All scores above " + LOWESTA + " earn an A!");
            }
        }
    1. What is the name of the class defined in the program above?
    2. List all of the classes (other than the one being defined) used in this program.
    3. List all of the object names used in this program.
    4. List all of the methods invoked in this program.
    5. List all of the variables used in this program. One is a constant, which one?
    6. What does this program do?