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.

    int sum = 0;
    for (int i = 0; i < scores.length; i++) {
        sum += scores[i];
    }


  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.

    Person(String name) {
        this.name = name;
    }



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

    ArrayList<String> names = new ArrayList<String>();


  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.

    try {
        FileReader reader = new FileReader("foo.txt");
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }









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

    int countParameters(int...parameters) {
        return parameters.length;
    }





  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.

    quitButton.addActionListener(new ActionListener( ) {
        @Override   // this line can be omitted
        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    });




  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.

    System.out.print(season);    // println is also acceptable



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

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

      Sprite frog = new Sprite();

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

      frog.start();

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

      public void run()     // also okay if marked with @Override


  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 complete JUnit 4 test method to make sure that it returns the correct average of 2.3 and 2.7.

    @Test
    public void testAverage() {
        assertEquals(2.5, Arithmetic.average(2.3, 2.7), 0.00001);
    }







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
      400

    2. 100 >> 2
      25

    3. 0xFF & 0x2A
      0x2A

    4. 0xFF | 0x2A
      0xFF

    5. 10 % 3 == 2 ? 'a' : 17    // yes, this is legal!
      17


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

    The dot operator



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

    When it is a method or constructor call or definition, or when preceded by a unary operator.



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

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

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

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

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

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

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

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

    1. DRY
      Don't Repeat Yourself

    2. TDD
      Test-Driven Design

    3. API
      Application Programmer's Interface

    4. JRE
      Java Runtime Environment

    5. XP (not the Microsoft OS)
      eXtreme Programming
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)?

    Calls a constructor for its superclass.

  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:
      public String toString()

    2. Benefit of overriding it:
      Simplifies printing, simplifies debugging

    3. Signature of another method:
      public boolean equals(Object o)

    4. Benefit of overriding it:
      Lets you test whether two distinct objects are equal
      Makes it possible to find an object in an array or collection


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

    Encourages small, single-purpose methods
    Discourages mixing computation and I/O
    Helps clarify what the method should do
    Ensures code is actually testable
    Helps keep methods relatively independent of one another, and of context




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

    So that each object is responsible for maintaining its own validity. Helps localize debugging.

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

    They should describe the state of the object. They should not be used for temporary values used in computation, or for passing information between methods within the class.
  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
      Audience: Other programmers/classes that may want to use your methods
      Information: Everything needed to use the methods correctly


    2. Comments inside a method
      Audience: Other programmers who may need to update, modify, debug, or maintain the method
      Information: Explanations of any code whose working isn't obvious to an average programmer