CIT591 Midterm Exam
Fall, 2003
Name_________________________________________

Please keep all your answers short and to the point. Do not provide extra information that was not asked for.

  1. (8 points) Distinguish between each of the following pairs of terms:

    1. Syntax / Semantics


    2. Operation / Method


    3. Declaring a variable / Defining a variable


    4. Formal parameter / Actual parameter



  2. (3 points) What is printed by the following code fragment?
    	    int p = 1;
    	    for (int i = 0; i < 5; i++) {
    	        p += i;
    	        System.out.print(p);
    	    }
    



  3. (3 points) The following complete program is supposed to compute and print out the product of two numbers (5 and 8). It contains one (major) syntax error and one logic error. Correct both.
    public class Product {
    	int x = 5, y = 8, p = 0;
        while (x > 0) {
    	    x--;
    	    p = p + y;
        }
        System.out.print(x + " times " + y + " equals " + p);
    }


  4. (6 points) What activities does each of the following software packages support?

    1. JRE

    2. JDK


  5. (4 points) Software is "elegant" if it combines what two characteristics?


  6. (4 points) Why must every Java application have a main method that is static?




  7. (8 points) What are the eight primitive types, and how many bytes long is each type?






  8. (6 points) What is the value of each of the following expressions?

    1. 12 / 5

    2. 2 + 3 * 4 - 5

    3. 10 << 2

    4. -8 % 3

    5. 3 | 5

    6. 3 ^ 5


  9. (5 points) Rewrite the following method, using as few symbols (words, operators, etc.) as possible.
    boolean ok(int[] array, int index) { boolean legal; if (index < 0) { legal = false; } else if (index >= array.length) { legal = false; } else { legal = true; } return legal; }
  10. (9 points) What is printed as a result of executing the following program? (Be careful to get the output lines in the correct order.)
    public class ArrayTest {
        public static void main(String[] args) {
            int[] test = new int[2];
            test[0] = test[1] = 5;
            System.out.println(test[0] + "," + test[1]);
            fiddle(test, test[1]);
            System.out.println(test[0] + "," + test[1]);
        }
        static void fiddle(int[] test, int element) {
            test[0] = 10;
            test[1] = 11;
            element = 12;
            System.out.println(test[0] + "," + test[1] + "," + element);
            test = new int[2];
            test[0] = 20;
            test[1] = 21;
            System.out.println(test[0] + "," + test[1]);
        }
    }
  11. (4 points) A break statement may only be used within what other kinds of statements?



  12. (5 points) Suppose you run your program and get the following error message:
    java.lang.NullPointerException
        at Test.run(Test.java:22)
        at Test.main(Test.java:6)
        at __SHELL1.run(__SHELL1.java:6)
        at bluej.runtime.ExecServer.suspendExecution(ExecServer.java:187)
        at bluej.runtime.ExecServer.main(ExecServer.java:69)
    Tell where in the program the error occurred, and what you expect to find when you look there.








  13. (3 points) Which executable statement is used primarily for documenting code, rather than for what it actually does?



  14. (5 points) True or false: In order to override a method:

    1. _____ The method must have the same name.

    2. _____ The method must be in the same class.

    3. _____ The method must have the same types of parameters.

    4. _____ The method must have the same access (public, protected, etc.).

    5. _____ The method must have the same return type.

  15. (5 points) True or false: In order to overload a method:

    1. _____ The method must have the same name.

    2. _____ The method must be in the same class.

    3. _____ The method must have the same types of parameters.

    4. _____ The method must have the same access (public, protected, etc.).

    5. _____ The method must have the same return type.


  16. (4 points) Write a (one) statement that sets the boolean variable quit to true if the String variable command has the value "quit", and sets quit to false for any other value of command.



  17. (2 points) How would you prevent a class that you write from being extended (subclassed)?



  18. (2 points) What is the kind (subclass) of Exception that doesn't need to be caught?


  19. (4 points) True or false:

    1. _____ All methods declared in an interface are automatically public and abstract.

    2. _____ An abstract class may contain methods that are not abstract.

    3. _____ A class may implement any number of interfaces.

    4. _____ An adapter class implements the methods of a given interface.


  20. (10 points; grading will be partly subjective) You have been hired to write a checkout system for a small library. They want to keep track of who has checked out which books, and when the books are due. In particular, they want to make sure no patron has more than 20 books at a time, and they want to send a detailed overdue notice to any patron with late books.

    Tell what classes you would define, what data should be in each class, and briefly describe the most important methods in each class. Do not write any code.