CIT 591 Midterm Exam, Fall 2007 Name ______________________________
  1. (5 points) What is the purpose of instance variables in a class?

    To describe the state of an object.

  2. (5 points) Why should instance variables normally be marked private?

    A class is responsible for protecting the validity of its objects. If they can be altered from outside the class, they can be made invalid.

  3. (5 points) How does the DRY principle apply to data?

    You shouldn't have redundant representations of data. If you need more than one representation, store it in a single representation, and provide methods to compute the alternate representations.

  4. (4 points) In Eclipse, a small red mark to the left of the Java code indicates a syntax error.

    1. What happens when you move the mouse (without clicking) over this mark?

      You get a description of the error or errors.

    2. What happens when you click on this mark?

      You get a list of suggested repairs.

  5. (2 points) What happens when you double-click on a tab in Eclipse?

    That pane expands to fill the entire window (or shrinks back into place).

  6. (2 points) What is the purpose of the JDK? That is, what does having it allow you to do?

    It allows you to create (compile) Java programs.

  7. (2 points) What is the purpose of the JRE? That is, what does having it allow you to do?

    It allows you to run Java programs.

  8. (3 points) What are the arguments, in the correct order, to the new Color(__, __, __) constructor?
    int red, int green, int blue

  9. (4 points) Write the Java operators for: "and" && , "or" ||, "not" !, "mod" %.

  10. (3 points) When should a method be static?

    When it neither uses any instance variables or methods, or changes any instance variables.

  11. (2 points) What does the acronym KISS stand for?

    Keep It Simple, Stupid (That means: Don't make things more complex than absolutely necessary)

  12. (2 points) What does the acronym YAGNI stand for?

    You Ain't Gonna Need It (That means: Don't write code you don't need right now.)

  13. (2 points) Suppose you have the following class definition:
         class Foo { int x = 3; int y = 5; static int z = 7; }
    and you create three Foo objects. How many ints have been created?
    Seven

  14. (3 points) Write a constructor for the above Foo class that takes one int parameter, and returns a Foo whose x value equals twice the given parameter.

    Foo(int n) { x = 2 * n; }

  15. (3 points) Write a method maxCoordinate for the above Foo class that returns the larger of the two values x and y.

    int maxCoordinate() {
         if (x > y) return x;
         else return y;
    }

  16. (3 points) Suppose names is an array of Strings. Write a loop to print out the contents of this array, one String per line.

    for (int i = 0; i < names.length; i++) {
         System.out.println(names[i]);
    }

  17. (2 points) What is the purpose of an assert statement? That is, when should you use it?

    To state a condition that you believe will be true whenever this statement is reached.

  18. (2 points) Write the declaration for a 9x9 array of integers named sudoku.

    int[ ][ ] sudoku = new int[9][9];
  19. (6 points) Give a very brief explanation of each of the parts of public static void main(String[ ] args):

    1. public     Available from anywhere

    2. static      Belongs to the class itself, not to individual objects

    3. void     Does not return a value

    4. main      The name of the method

    5. String[ ]      The single formal parameter is an array of Strings

    6. args      The name of the formal parameter

  20. (5 points) Consider the following program:
    int syntaxErrors(int input) {     // Must declare the type of the parameter
         int maximum = 100; // Error fix; see below
         private int count = 0;     // Local variables cannot be private

         do {

               int maximum = 100;     
    // Declaration here is okay but causes error below

              input = 2 * input;

              count++;

         } while (input <= maximum);
      // maximum is out of scope; declaration is in loop

         return count;
    }
    1. There are three syntax errors in the above method. Find and fix them.

    2. Assuming the errors have been fixed, write a single statement to call this method with the parameter 10 and print the result.

      System.out.println(syntaxErrors(10));

    3. What is printed by the statement in part (b) of this question?
      4

  21. ( 3 points) Why is it a bad idea to do both computation and input/output in the same method?

    It prevents you from using the method later if you want to do the computation without the input/output.

  22. (2 points) What, in Java, does a method signature consist of?

    The name of the method, and the number, order, and types of parameters

  23. (4 points) Mention two ways in which writing tests before writing the code to be tested (rather than writing them afterwards) is likely to improve the quality of the program.

    Methods are more likely to be single purpose.
    The purpose of the method is likely to be more clearly defined.
    Methods are more likely to be testable.
    Methods are likely to be more independent of context.
       Note: The question was NOT "How does testing improve the quality of the program?"


  24. (10 points) Tell, very briefly, what each of the following keyword means or does.

    1. static     Says it belongs to the class, not to individual objects


    2. this     The object that is executing the current code


    3. void     Specifies that a method does not return a value


    4. break     Exits from a loop or a switch statement


    5. continue     Goes to the next iteration of the enclosing loop


  25. (4 points) In pair programming,

    1. What is the role of the "driver"? That is, what does he or she do?

      To do the typing and actually implement the program.

    2. What is the role of the "navigator"?

      To watch and direct the driver, and to think ahead about what should be done next.


  26. (2 points) What is the name of the program that produced the Java API documentation?

    javadoc

  27. (2 points) Add one line to complete the following constructor.

    class Person {
         String name;

         Person(String name) {

                   this.name = name;
         }
    }


  28. (3 points) Suppose you get a NullPointerException for the following statement:
         System.out.println(myFriend.spouse.getBirthday());
    What can you say about the probable cause of the error?

    Either myFriend or spouse is null.

  29. (2 points) What does a "unit test" test?

    The methods in a single class.

  30. (3 points) What package is automatically loaded into your Java program, without needing to explicitly import it?

    java.lang