CIT 590 Midterm Exam Name ______________________________
Spring 2009

I. Writing code

Where I ask for a statement, give me a statement, not a complete method. Where I ask for a method, give me a method, not a class. And so forth.

  1. (5 points) Write a declaration of variable foo to be of type double with an initial value of 17.76.

  2. (5 points) Declare a variable students to be an array of Student objects, but don't give it an initial value.

  3. (5 points) Declare and define a variable books to be an ArrayList of Book objects. Use the correct syntax for generics (that is, make sure your ArrayList will hold Books and nothing else).

  4. (5 points) Write a public "stub" method (one that does nothing) named fiddle that takes no arguments and returns no value.

  5. (5 points) Write a public "stub" method named faddle that takes two integer arguments and returns a String. Although this method does nothing, it should be complete enough to compile without error.

  6. (5 points) Write the shortest possible statement to add one to the variable count.

  7. (5 points) Write the shortest possible statement to add five to the variable count.

  8. (5 points) Write the shortest possible statement to return true from a method if x is greater than 5, and false otherwise.

  9. (5 points) Write the shortest possible statement that adds 1 to index, but resets index to zero if adding 1 would make it equal to length.

  10. (5 points) Write a loop, and any additional necessary statements, to set the integer variable sum to the sum of all the values in the one-dimensional array scores of integers.

  11. (5 points) Write a switch statement to set the String variable word to either "one", "two", or "many", depending on whether the integer variable count is 1, 2, or some other value, respectively.

  12. (5 points) Write a single statement to create and throw an RuntimeException if the variable k is negative.

II. Short answer

Keep all answers brief and to the point. Do not include information that was not asked for--it may cost you points. You may be penalized for excesssively verbose answers.

  1. (5 points) Instance variables should usually be private. Why?

  2. (10 points) Suppose you have a method which only works correctly if a certain double parameter is between 0.1 and 1.0. As the first step of your method, you want to ensure that this parameter is in the legal range. There are two ways to do this:

    1. If the method is public, instead of using an assert statement, you should throw some kind of Exception for illegal values of the parameter. Why?

    2. If the method is private, instead of throwing an Exception, you probably should assert that the value of the parameter is in the legal range. Why?

  3. (5 points) Point out the syntax error in the following code, and tell how to correct it.
    Random rand = new Random();
    do {
         int row = rand.nextInt(10);
         int col = rand.nextInt(10);
    } while (row == col);

  4. (5 points) The keyword this cannot be used in a static method. Why not?

  5. (5 points) When a constructor is called, what is the first thing that it does (whether you tell it to or not)?

  6. (5 points) If a class implements an interface, what is that class "promising" to do?

  7. Consider the following method:
    public String toString() {
        top = (top & 0x3FF) % 900 + 100;
        left = (left & 0x3FF) % 900 + 100;
        right = (right & 0x3FF) % 900 + 100;
        bottom = (bottom & 0x3FF) % 900 + 100;
        System.out.println("[" + top + " " + left + " " + bottom + " " + right + "]");
    1. (5 points) What style rule or rules does this method violate? How could we improve the style?

    2. (5 points) Besides poor style, this method is committing a serious logic error. What is it, and how could we fix it?