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. _____ Every object belongs to some class.

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

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

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

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

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

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

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

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

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

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

    12. _____ An interface can implement another interface.

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

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

    15. _____ Local variables may be declared private.

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

    Statements
    Result
    a.
    int x = 5;
    x = x++;
    System.out.println(x);

    b.
    int x = 2;
    while (x < 100) x *= 5;
    System.out.println(x);

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


    d.
    int x = 2;
    switch(x) {
        case 1: x = 7;
        case 2: x = x + 1;
        case 3: x = 5 * x;
        default: x = -x;
    }
    System.out.println(x);


    e.
    int x = 2;
    do { x *= x; } while (x < 100);
    System.out.println(x);

    f.
    int x = 1;
    for (x = 1; x < 5; x = x + 2) x--;
    System.out.println(x);

    g.
    int x = 2;
    if (++x > 0 || ++x < 5) x = -x;
    System.out.println(x);

    h.
    String s = "abcdefghij";       
    System.out.println(s.substring(3, 6));

    i.
    int a = 1, b = 4, c = 2, d = 3;
    System.out.println(a + b + "=" + c + d);

    j.
    Random rand = new Random();
    System.out.println(rand.nextInt(1));


  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.



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



    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."



    6. Print  true if variable myDog holds an instance of the Poodle class, and print false otherwise.



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




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



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



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



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



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




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

    1. TDD


    2. DRY


    3. MVC


  5. (4 points) List two advantages of writing tests before you write the code. (I am not asking for advantages of just having tests!)






  6. (3 points) Name and very briefly describe three types of layout manager. Use correct capitalization.






  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)








    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:



              // 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.











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




  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.








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





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









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






  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.)



  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.)