CIT591 Midterm Exam
Fall, 2005
Name  Answer Key  

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

  1. (8 points) Display could be the name of a class or interface , while display could be the name of a variable or method .

  2. (2 points) "Variables should have meaningful names." Mention one exception to this rule.
    Index of a for loop; local variables used briefly with obvious meaning; or variables with no special meaning

  3. (4 points) Rewrite the following code fragment, using the style rules defined for this CIT591 class. Use a box () to indicate a space.

  4. (4 points) A method declared in an interface is implicitly public and abstract .

  5. (4 points) If your only constructor for a class takes an int parameter, what must be true of all the subclasses of that class?
    Every subclass constructor must explicitly call the superclass constructor.

  6. (6 points) Circle the statement types that require the use of { } braces.
    while if-then switch ({} required)
    do-while ({} required) for try-catch ({} required)
  7. (2 points)Which package is automatically imported into every Java program?

  8. (6 points) Tell what is printed by each of the following:
    1. for (int i = 4; i < 10; i += 2) { System.out.print(i + " "); }
      4 6 8

    2. int i = 4; while (i < 10) { i++; System.out.print(i + " "); }
      5 6 7 8 9 10

    3. int i = 4; do { System.out.print(i + " "); i++; } while (i < 10);
      4 5 6 7 8 9

  9. (15 points) True or False:
    1. True  You can write a constructor for an abstract class.
    2. False  You can create an instance of an abstract class.
    3. False  If AbsClass is an abstract class, the expression x instanceof AbsClass is always false.
    4. False  You can declare a constructor in an interface.
    5. False  You can override an inherited method to make it private.
    6. False  You can override an inherited method and throw additional checked exceptions.
    7. True  You can override an inherited method and throw fewer checked exceptions.
    8. True  It is legal to have methods void foo(String x) and int foo(Object x) in the same class.
    9. False  It is legal to have methods void foo(String x) and int foo(String y) in the same class.
    10. True  Classes, methods, and instance variables can all be declared private.
    11. True  Classes, methods, and instance variables can all be declared final.
    12. True  A class can extend only one other class.
    13. False  A class can implement at most one interface.
    14. True  A member class can access private instance variables of the enclosing class.
    15. False  A local inner class can access local variables of the enclosing method.
  10. (5 points) Given: interface Soap { int wash(String s); }
    Write a class that implements this interface.
    class IvorySoap implements Soap {
        public int wash(String str) {   // can use any name for parameter
            return 0;   // must return an int value
  11. (5 points) In class Example, write a constructor Example() that calls another constructor Example(int x) with a zero argument.
    Example() {   // can also be public, protected, or private

  12. (3 points) What is printed by the following? (Trick question--be very, very careful!)
    int x = 0;
    for (int i = 0; i < 5; i++) { x = x++; System.out.print(x); }

  13. (5 points)In Swing, create a JFrame with a layout that looks something like the following. You can assume that buttons or something will be put in each area; that's not part of this question.
    // Simplest solution
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();
    panel.setLayout(new GridLayout(4, 1));
    frame.add(panel, BorderLayout.EAST);
    (Can use BoxLayout instead of GridLayout, but then must add panels)

  14. (4 points) Briefly, what does it mean to say that a dialog is modal?
    The program does not continue until the dialog box is closed.

  15. (6 points) If you needed to check whether a completed Sudoku puzzle is correct, what would you need to check? (Answer in English, not Java code!)
    Each row, column, and "box" contains all nine digits.

  16. (3 points) Java uses several different namespaces. List three of them.
    Any 3 of: packages, types (classes & interfaces), fields, methods, local variables, labels, and enums.

  17. (6 points) There are three main steps to using a file. What are they?

    Open, use, close.

  18. (6 points) Briefly, what is the difference between:
    1. >> and >>>
      >> extends the sign bit; >>> is zero fill

    2. && and &
      && is short-circuit; & is not (and is used for bit manipulation)

    3. x++ and ++x
      x++ increments after using the value of x, ++x increments before using it

  19. (6 points) Suppose you have a class Person with instance variables String firstName and String lastName. Override the following methods:
    1. Override toString to return a person's full name.
      public String toString() { // must use this exact signature
          return firstName + " " + lastName;

    2. Override equals to return true if two people (Person objects) have the same first and last names.
      public boolean equals(Object o) { // must use this exact signature
          if (o == null) return false;
          String other = (String)o;
          return firstName == other.firstName && lastName == other.lastName;