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
      Syntax describes the grammatical rules of a language;
      Semantics describes the meaning of constructs in a language


    2. Operation / Method
      Operations are predefined in the language, and apply to primitives;
      Methods may be defined by the programmer, and apply to objects


    3. Declaring a variable / Defining a variable
      Declaring a variable associates a type with the variable;
      Defining a variable allocates space for a value of that type

    4. Formal parameter / Actual parameter
      Formal parameters are local variables declared in the method header;
      Actual parameters are values given to the formal parameters by the method call


  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);
    	    }
    
              124711

  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);
    }
    Syntax: Not in a method; surround with public static void main(String[] args) { }
    Logic: Prints "0 * 8 = 40"--define int temp = x, use temp in loop

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

    1. JRE
      Running Java programs
    2. JDK
      Creating Java programs--most importantly, compiling them. (Does not include an editor!)

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

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

    It must be static because, when Java first begins, there are no objects to respond to messages. You need a static (class) method to get started.


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

    byte: 1 byte
    short: 2 bytes
    int: 4 bytes
    long: 8 bytes
    boolean: 1 byte
    char: 2 bytes
    float: 4 bytes
    double: 8 bytes

    Where did so many people get the bizarre idea that a boolean requires 4 bytes?

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

    1. 12 / 5        2

    2. 2 + 3 * 4 - 5   9

    3. 10 << 2    40

    4. -8 % 3    -2

    5. 3 | 5    7

    6. 3 ^ 5    6


  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; }
    boolean ok(int[] array, int index) { return index >= 0 && index < array.length; } // I gave some points for making the method shorter, if it was still correct.
  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]);
        }
    }
    5, 5 10, 11, 12 20, 21 10, 11
  11. (4 points) A break statement may only be used within what other kinds of statements?

    Loops and switch statements (Or: for, while, do-while, and switch)

  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.

    It occurred in line 22 of Test.run (and no, it did not occur at line 6 of Test.main)

    What you should expect to find is an attempt to use a method or field of an object (using dot notation); the error is because the object is null.

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

    assert

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

    1. true  The method must have the same name.

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

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

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

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

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

    1. true  The method must have the same name.

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

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

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

    5. false 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.

    Correct: quit = "quit".equals(command);
    Partly correct: quit = command.equals("quit");

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

    Mark it final


  18. (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.

Obvious classes are Book and Patron.

A Book should have a title, author, and due date, and probably other information, such as ISBN and/or Dewey Decimal number. (By the way, a Dewey Decimal number does not uniquely identify a book.) You might also want a unique ID for this particular copy of the book.

A Patron should have a name, an address, and a list of Books that he/she has checked out.

Somebody, or something, needs to take responsibility for checking books out, accepting returned books (a lot of people forgot this!), and sending notices for overdue books. I called this class the Library; a popular name seems to be CheckoutSystem or similar.

class Book {
    String title
    String author
    Date dueDate
    // Constructor, getters for each field
}

class Patron {
    String name
    String address
    Book[] books = new Book[20]
    int numberOfBooks
    // methods
    getters for name and address
    void takeBook(Book)
    void returnBook(Book)
    int getNumberOfBooks()
    Book[] getOverdueBooks()
    sendNotice()
}

class Library {
    Book[] books  // quite large
    Patron[] patrons
    // methods
    boolean checkOut(Patron, Book) // fails if too many books
    void checkIn(Patron, Book)
    void checkForOverdueBooks(Patron)
}