CIT 591 Final Exam, Fall 2008 Name ______________________________
  1. (5 points) Write the complete header for a program's main method.

    public static void main(String[] args)

  2. (5 points) Write a complete enum class for the days of the week. Use three-letter abbreviations for the days.

    enum Days { SUN, MON, TUE, WED, THU, FRI, SAT }

  3. (15 points) Consider the following method (defined in a class WordPlay):
        public static boolean isVowel(char ch) {
            return "aeiouAEIOU".indexOf(ch) > 0;
    1. Write a complete JUnit test method for the isVowel method.
      public void testIsVowel() {
          assertTrue(WordPlay.isVowel('a')); // must test 'a'
          assertTrue(WordPlay.isVowel('U')); // should test 'U'
          assertFalse(WordPlay.isVowel('w')); // test both true and false
    2. Write a complete Javadoc comment, using the correct syntax, for the isVowel method.
       * Tests whether its argument is a vowel.
       * @param ch The character to be tested.
       * @return True if the argument is a vowel.
       */      // It is incorrect to use @author, @version, etc., here
    3. Why is making this method static the correct thing to do?

      Because it uses no instance variables or instance methods of the WordPlay class.

  4. (5 points) A class has a private instance int[ ] variable named position. Write a getter method for position.

    public int[] getPosition() {
        return position;

  5. (5 points) Two Book objects are "equal" if they have the same Isbn, where Isbn is an instance variable of type String. Write a correct and complete equals method for Book objects, and indicate (with a @ annotation) that it overrides the inherited equals method.
    public boolean equals(Object obj) {
        if (!(obj instanceof Book)) {
            return false;
        Book otherBook = (Book) obj;
        return Isbn.equals(otherBook.Isbn); // or can use getters

  6. (15 points) True or False:

    1. True  Every class always has a constructor.

    2. False If Inner is an inner class of class Outer, then objects of type Inner can be used only within class Outer.

    3. False A static method may not be private.

    4. False A method that returns void cannot be JUnit tested.

    5. False Declaring (but not defining) a JPanel object does not allocate any space.

    6. True  An instance method may have the same name as an instance variable.

    7. False A method that takes a double parameter can only be called with a float or double argument.

    8. True  A constructor may call another constructor with one of the keywords this and super.

    9. True  An abstract class may have an explicit constructor.

    10. False An interface may declare a constructor.

    11. True  All enum constructors are automatically private.

    12. False An interface can implement another interface.

    13. False If the same variable name is used in several methods, it should be an instance variable.

    14. True  When printing an object, System.out.println calls the object's toString() method.

    15. True  Scope rules apply within a class; access rules apply between classes.

  7. (5 points) String s contains only digits. Write a single assignment statement to convert this string to an integer and assign it to variable n.
    n = new Integer(s).intValue();    // or
    n = Integer.parseInt(s);

  8. (5 points) Write a single print statement to print true if the double variable x is greater than 100.0, and print false otherwise.
    System.out.print(x > 100.0);
    System.out.print(x > 100.0 ? "true" : "false");    // legal but silly

  9. (5 points) Write a single print statement to print the value of x if it is positive, and print the value of y otherwise.

    System.out.print(x > 0 ? x : y);

  10. (5 points) Draw a vertical line, |, everywhere in the following statement that the style rules specify there should be a space. (I've put one in the only location required by the syntax rules.)
               f  o  r | (  i  n  t    |    i | = | 0  ; | i | <  = | 1  0  ; | i  +  +  ) | s  u  m | +  = | i  ;

  11. (10 points) Give two reasons why it is a good idea to write tests before writing the code to be tested.
    Helps clarify what the code is supposed to do.
    Code is written to be testable.
    Encourages simpler, single-purpose methods.
    Encourages code that is relatively independent of its environment.

  12. (5 points) What should a constructor do, and what shouldn't it do?
    It should construct the object in a valid state.
    It shouldn't do anything else.

  13. (5 points) For whom should you write Javadoc comments, and what kind of information should you convey?
    Write for other programmers who are going to use your classes and methods.
    Tell them everything they need to know to use your code (and nothing more).

  14. (5 points) For whom should you write internal (// and /*...*/) comments, and what kind of information should you convey?
    Write for other programmers who are going to modify and update your classes and methods.
    Explain anything that isn't obvious about how the code works.
  15. (5 points) Variable rabbit holds an object of type Animal, which implements Runnable. Write the code to start rabbit executing in a new Thread.
    Thread th = new Thread(rabbit);
    new Thread(rabbit).start();