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.
    Comments are used to make programs more understandable and explain their purpose. You get multi-line comments by using /* to start the comment and */ to end it. End of line comments are obtained using // where anything after the // (on teh same line) is a comment.
  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).
    r = total % 25;
  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;
      For part a, the order is red (*), blue (/), green (+), black (-) and then assignment is done last (=). Intermediate results are 7 + 18 / 5 - 1, then 7 + 3 - 1, then 10 - 1, and finally 9 is assigned to n. For part b, the order is %, then *, then /, then +, then -, and lastly assignment or =. Some of the intermediate results are 0 + 4 - 1, then 4 - 1, and finally 3 is assigned to n.
  4. Write Java statements (or one statement) to declare and create a variable of the class MainWindow.
    MainWindow pella = new 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);
      (a) results in 10.0 / 4 + 5.0 which is 7.5.
      (b) results in (double) (1) which is 1.0.
      (c) results in -14.0 / 10, or -1.4.
      (d) gives 25.0.

  6. Give brief definitions or explanations of the following terms:
    1. CPU                         d. high level language
    2. bytecode                   e. compiler
    3. bit                              f. double
      CPU stands for central processing unit, it is the brains of the computer.
      bytecode is Java's version of machine language, it is what the Java Virtual Machine interprets, and id therefore like the version of a program that the computer executes.
      bit is a binary digit, i.e. a zero or a one.
      high level language is a programming language like Java that humans can read, but computers cannot.
      compiler is the software (program) that translates source code into bytecode.
      double is a primitive data type in Java for storing high precision decimal numbers.

  7. What is the main difference between an object of some class and a primitive data type (variable)?
    Objects are stored as a reference to another memory location (where the actual data is kept), while primitive data types are stored by their values. If fred = 4 is an int, and wilma is a MainWindow, then in memory location named fred, you will find 4, while in memory location wilma, you will the address in  memory, where all of wilma's properties are stored.
  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

    The valid identifiers are  myTurtle, SNapPY, x8, noodleMania4, and MrMagoo. Of these, SNapPY and MrMagoo violate conventions for naming objects, since object names should begin with a lowercase letter.
  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.
    wilma.moveHorizontal(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;
    in order we get:
    num = 398
    total = 8
    num = 39
    total = 17
    num = 3
    total = 20

  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).
    talky.show(weight + " ounces");
  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.
    Click here to see the source code.

  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? It must be Greeting.java.
  17. Convert (a) and (b) below from binary to decimal (base 10), and (c) and (d) from decimal to binary (base 2).
    1. 1011 this is 1 + 2 + 8 = 11 base 10.
    2. 111000 this is 8 + 16 + 32 = 56 base 10.
    3. 32 in base 2 this would be 100000.
    4. 50 in base 2 this would be 110010.
  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? Silly
    2. List all of the classes (other than the one being defined) used in this program.
      MainWindow, InputBox, OutputBox and Clock.
    3. List all of the object names used in this program.
      glassy, gabby, asky, and ticky.
    4. List all of the methods invoked in this program.
      There are 4 constructor methods (all follow new), Mainwindow(), OutputBox(), InputBox(), and Clock(). Then, show(), printLine(), pause(), getInteger(), and skipLine() are all instance methods.
    5. List all of the variables used in this program. One is a constant, which one?
      The variables are score1, score2, score3, avg, and LOWESTA, the last being a constant.
    6. What does this program do? It asks the user for three exam scores, calculates and prints their average, and then explains that all averages above 90.0 earn an A.