CIT 590 Final Exam Name ______________________________
Spring 2009

I. Writing code

Write only the code requested. 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. Don't print results unless I ask you to. And so forth.

  1. (5 points) Write a statement or statements to set sum to be the sum of the numbers in the array int[ ] scores.

  2. (5 points) A program contains the statement

         Person p = new Person("John Smith");

    Write the constructor you would put in the Person class to handle this statement.

  3. (5 points) Declare and define a variable names to be an ArrayList of Strings.

  4. (5 points) The statement

          FileReader reader = new FileReader("foo.txt");

    may throw a FileNotFoundException. Put this statement in a try statement, and print a stack trace if the exception occurs.

  5. (5 points) Write a method countParameters that takes any number of int parameters, and returns how many parameters it was called with.

  6. (5 points) Suppose quitButton is a JButton. Use an anonymous inner class to attach a listener to quitButton that will end the program when the button is clicked.

  7. (5 points) Suppose Season is defined as

          enum Season { WINTER, SPRING, SUMMER, FALL }

    and season is a variable of type Season. Write the shortest possible statement to print out the value of season.

  8. (5 points) Assume that the Sprite class extends Thread.

    1. Declare and define a variable frog of type Sprite.

    2. Write a statement that will cause the frog Thread to begin execution.

    3. Write the header (first line) of the method that will execute when the frog Thread is started.

  9. (5 points) The method

          public static average(double d1, double d2)

    defined in class Arithmetic, is supposed to return the average of its two parameters. Write a JUnit 4 test method to make sure that it returns the correct average of 2.3 and 2.7.

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. (10 points) Tell the result of each of the following expressions:

    1. 100 << 2

    2. 100 >> 2

    3. 0xFF & 0x2A

    4. 0xFF | 0x2A

    5. 10 % 3 == 2 ? 'a' : 17

  2. (2 points) Style: Which binary operator should not be surrounded by spaces?

  3. (2 points) Style: An open parenthesis, (, should normally be preceded by a space. State one exception to this rule.

  4. (6 points) Mark each statement as either true (T) or false (F).

    1. _____ In an abstract class, all methods are abstract.

    2. _____ The Serializable interface doesn't declare any methods.

    3. _____ The @author tag is used for classes, interfaces, and methods.

    4. _____ Style: Methods should not contain both computation and printing.

    5. _____ Style: JUnit tests should not print anything.

    6. _____ Objects of an inner class type can be used in unrelated classes (that is, other than the enclosing class).

  5. (5 points) What do each of the following acronyms stand for?.

    1. DRY

    2. TDD

    3. API

    4. JRE

    5. XP (not the Microsoft OS)

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

  2. (6 points) Some methods inherited from Object by your own classes should normally be overridden. Give the complete signature of two such methods, and for each, tell why overriding it is a good idea.

    1. Signature of one method:

    2. Benefit of overriding it:

    3. Signature of another method:

    4. Benefit of overriding it:

  3. (4 points) Give two reasons why writing tests first tends to improve the quality of code.

  4. (5 points) In general, instance variables should be private. Why?

  5. (5 points) What should the fields (instance variables) of an object describe, and what should they not be used for?

  6. (5 points) Tell, for each of the following kinds of comments, who your intended audience should be, and what kind of information you should convey.

    1. Javadoc comments

    2. Comments inside a method