CIT 591 Final Exam, Fall 2007 Name ______________________________

Please keep answers brief and to the point. Adding information that I did not ask for will not improve your grade, but will cost you points if you get it wrong. In answers where case matters, be sure I can tell your capital letters from your lowercase letters.

  1. (15 points) True or False:

    1. True  Every object belongs to some class.

    2. True  Every class (except Object) extends one other class.

    3. False Classes inherit the fields, methods, and constructors of their superclass.

    4. True  A method may have the same name as an instance variable.

    5. False Static fields of a class are available only in static methods, not instance methods.

    6. False Declaring an object (for example, Person p;) allocates space for that object.

    7. True  An instance variable may have the same name as a method in the same class.

    8. True  An instance variable may have the same name as the class in which it occurs.

    9. False A static variable may have the same name as an instance variable in the same class.

    10. False The signature of a method consists of its name and the order, names, and types of its parameters.

    11. True  Formal parameters may be declared to be final.

    12. False An interface can implement another interface.

    13. True  A method that is declared with an int parameter may be called with a char value.

    14. True  You can put a return statement in a constructor.

    15. False Local variables may be declared private.

  2. (20 points) For each of the following groups of statements, tell what is printed.

    int x = 5;
    x = x++;
    int x = 2;
    while (x < 100) x *= 5;

    int x = 10;
    System.out.println(x < 5 ? "A" : x > 10 ? "B" : "C");

    int x = 2;
    switch(x) {
        case 1: x = 7;
        case 2: x = x + 1;
        case 3: x = 5 * x;
        default: x = -x;

    int x = 2;
    do { x *= x; } while (x < 100);
    int x = 1;
    for (x = 1; x < 5; x = x + 2) x--;
    int x = 2;
    if (++x > 0 || ++x < 5) x = -x;
    String s = "abcdefghij";       
    System.out.println(s.substring(3, 6));
    int a = 1, b = 4, c = 2, d = 3;
    System.out.println(a + b + "=" + c + d);
    Random rand = new Random();

  3. (24 points) In each of the following, write a Java statement to perform the required task. Do not write complete classes or complete methods.

    1. Print out the first character in the string s.


    2. myArray is a rectangular two-dimensional array of strings. Set the int variable size to the total number of locations in the array.

      size = myArray.length * myArray[0].length;

    3. Set boolean variable b to true if string s contains the letter 'A', and to false otherwise.

      b = s.indexOf('A') >= 0; b = s.lastIndexOf('A') >= 0; b = s.contains("A");
      and variations of these

    4. Disable the JButton named clearButton (so that the user cannot click it).


    5. Create and throw a runtime exception with the message "Insufficient room".

      throw new RuntimeException("Insufficient Room");

    6. Print  true if variable myDog holds an instance of the Poodle class, and print false otherwise.
      System.out.println(myDog instanceof Poodle);

    7. Notify all observers that a change has occurred in "this" object (requires two statements).

      notifyObservers(); // may take an Object as an argument

    8. Create a new array named odds containing the first five odd positive integers.

      int[] odds = { 1, 3, 5, 7, 9 };   or   int[] odds = new int[]{ 1, 3, 5, 7, 9 };

    9. Exit an enclosing loop if the variable score is negative.

      if (score < 0) break;

    10. Set int variable cost to the rounded value of the double variable price.

      cost = (int) (price + 0.5);   or   cost = (int) Math.round(price);

    11. Create an ArrayList named names that can hold only strings.

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

    12. Set the object obj to null unless it is an instance of a class that implements the Comparable interface.

      if (!(obj instanceof Comparable)) obj = null;

  4. (3 points) What words do each of the following acronyms stand for?

    1. TDD    Test Driven Design

    2. DRY     Don't Repeat Yourself

    3. MVC     Model-View-Controller

  5. (4 points) List two advantages of writing tests before you write the code. (I am not asking for advantages of just having tests!)
    Clarifies what the methods are supposed to do
    Methods are testable
    Methods tend to be simpler and single-purpose
    Methods tend to be more independent of the environment
    Tests get written

  6. (3 points) Name and very briefly describe three types of layout manager. Use correct capitalization.
    FlowLayout -- arranges components left to right, top to bottom
    GridLayout -- arranges components in a rectangular grid
    BorderLayout -- uses five areas: NORTH, EAST, SOUTH, WEST, and CENTER
    GridBagLayout -- gives very complex control over arrangement
    BoxLayout -- arranges components horizontally or vertically
    CardLayout -- arranges components as a stack of cards
    OverlayLayout --arranges components on top of one another
    SpringLayout -- arranges components according to constraints

  7. (5 points) Write a complete Javadoc comment for the following method. Use the correct syntax, and include all appropriate tags.

    (Write your Javadoc comment here)
     * Tests whether its input parameter is odd.
     * @param n The number to be tested.
     * @return true if the number is odd, false otherwise.
    static boolean isOdd(int n) {
        return n % 2 == 1;

  8. (4 points) Write a JUnit method (not an entire class!) to test the method in the previous question. Assume the isOdd method is in class MyMath.

        // Write the method header:

        public void testIsOdd() {

              // Write a statement to test whether the method returns true for the number 1.


              // Write a statement to test whether the method returns false for the number 0.


              // Write a statement to test whether the method returns true for the number -1.



  9. (6 points) List three things to do to make a method more readable.
    Keep it short--should be able to see the entire routine at a time
    Have it do a single thing
    Give it a meaningful, descriptive name
    Write comments that further clarify what the method does
    Have it call methods with meaningful, descriptive names
    Use established idioms
    Give it a short, memorable parameter list, with well-chosen parameter names
    Format it properly, following established conventions
    Donít do ďweirdĒ things
    Donít change parameter objects unnecessarily
    If available outside the class, make it work for any valid object
    Donít make it depend on some other method being called first

  10. (2 points) Write the complete signature of the method that must be implemented by any class that implements the Observer interface.

    void update(Observable o, Object arg)

  11. (3 points) Assume that class Person has an instance variable String name, and assume that variable friend holds an object of type Person. Write the complete method you need to add to the Person class in order to make the statement System.out.println(friend) print out that person's name.

    public String toString() {
        return name;

  12. (2 points) Sometimes a constructor is syntactically required to make an explicit call to a superclass constructor. What would make this necessary?

    If the superclass has explicitly defined constructors, not including one with no arguments.

  13. (4 points) In pair programming, what are the two roles, and what is the person in each role supposed to do?

    The "driver" controls the keyboard and mouse, and enters the code.
    The "navigator" (or "observer") observes, asks questions, suggests solutions, and thinks about slightly longer-term strategies.

  14. (3 points) Briefly define the term refactoring.

    Changing the organization of a program without affecting its behavior.

  15. (1 point) Who was the best partner you had this semester? (If you're unsure of his/her name, just give the name of the assignment instead.)

    Full credit for giving one or more names.

  16. (1 point) Who was the worst partner you had this semester? (If you're unsure of his/her name, just give the name of the assignment instead.)

    Full credit for giving one or more names.