CS56—Midterm Exam 1
E01, W12, Phill Conrad, UC Santa Barbara
02/07/2012

Name: ___________________________________________________


Umail Address: ______________________________@ umail.ucsb.edu



Circle one:       2pm        3pm        9am   



Please write your name only on this page.
That allows me to grade your exams without knowing whose exam I am grading.

This exam is closed book, closed notes, closed mouth, cell phone off,
except for:

There are 100 points worth of questions on the exam, and you have 75 minutes to complete the exam.

A hint for allocating your time—on your first pass through the exam:

If you do that, after you complete your first pass through the exam in 50 minutes, you'll still have 25 minutes to:


  1. (50 pts) In the space below (and on the next page if needed) write the code for the TideReading class,
    according to the instructions on the separate handout provided.

    There is more room on the next page for your answer if you run out of room here.

    Extra space for your answer to question 1

  2. Assume that we have the TideReading class that you defined in question 1, with ONLY the constructor and the methods that were required for that problem.

    What would the output of the following main program be? In addition to the output, provide a brief explanation.

     public class E01q2 {
     
       public static void main(String [] args) {
             TideLevel a = new TideReading('H',3.4);
             TideLevel b = new TideReading('L',0.1);
             TideLevel c = new TideReading('L',0.1);
             TideLevel d = b;
             TideLevel e = c;
             
             System.out.println("a=" + a);
             
             if (a.equals(b))
                System.out.println("awesome");
             if (b.equals(c))        
                System.out.println("super");
             if (b == d)        
                System.out.println("puppy");
             if (b.equals(e))        
                System.out.println("bowl");
             if (a.equals(d))        
                System.out.println("parade");
             if (b == c)        
                System.out.println("party");
             if (d == e)        
                System.out.println("friends");
             if (e.equals(d))        
                System.out.println("rock!");
    
    }

    Output (6 pts):

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    Explanation (4 pts):

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
  3. (2 pts) Consider the variable a in the main program above. Is the variable a on the stack, or on the heap? Circle one:    stack     heap

  4. Consider the private instance variable that represents the tide level of the object referred to by the reference a.

    1. (2 pts) Is that private instance variable on the stack or the heap? Circle one:    stack     heap

    2. (2 pts) Is that private instance variable of a primitive type or a reference type?      primitive     reference
  5. When using a library such as JUnit which is NOT part of the standard Java Development Kit (i.e. a third-party library), there is something that one must do to be sure that the java compiler, the java virtual machine, and the javadoc utility have access to that library.

    1. (2 pts) Can you describe in general terms what one must do to make third-party libraries available to the compiler, JVM and javadoc utility? (Read all three parts of the question (a), (b) and (c) before starting to write your answer.)





    2. (2 pts) Regarding your answer to (a)—there are at least two ways this can be done WITHOUT using Ant. Describe one of these.




    3. (4 pts) Regarding your answer to (a)—how is this done when you ARE using Ant?

      (Don't write the specific lines of XML that accomplish this—just describe in English what has to be done. However, for full credit, each of the following technical terms must appear and be used correctly in your answer: target, task, nested XML element, JAR file)





  6. (4 pts) Consider the following Java method that appears inside a class Dog.

    Assume that the eat() method of a DogBowl empties the bowl, and returns a number of "health points" for the Dog (or zero if the DogBowl is already empty.)

    Assume that everything else about the Dog class is correct, except for this sketchy method.

    What is sketchy about this code? (Describe in a few sentences of English.)


    /** Feed the dog.  The food in the DogBowl passed in will be eaten, and 
        the health of the dog will increase depending
        on the nutrition level of the dog food that was in the bowl (if any).
        
     @param db A bowl of Dog Food.   
     */
    
    public void feed(DogBowl db) {
       DogBowl db = new DogBowl();
    
       this.increaseHealthBy(db.eat());
    }
    
    
    
  7. (4 pts) Mary and Fred are arguing whether javadoc is a "target" or a "task" in Ant. You want to keep the peace between Mary and Fred, because you share a crowded apartment with them on Del Playa, and when they aren't happy, no-one in the house is happy.

    What can you say to them to settle the argument, but allow both of them to "save face"?


     

  8. Consider the #include directive in C/C++ vs. the import statement in Java.

    1. (4 pts) In what ways are #include and import similar in the role they play in the language?












    2. (4 pts) In what ways are #include and import different in how they are actually implemented?

      Be as specific and precise as you can be—a very vague or general answer, even if correct, may be worth only partial credit.












    There are more questions on the back page—dont forget them!

    For the remaining questions, you only need to write individual fragments of java code.
    Don't worry about putting your code inside a method or a class.

  9. (2 pts) Write a line of Java code that declares a variable x that is NOT of a reference type.


     

  10. (2 pts) Write a line of Java code that invokes autoboxing, but does NOT invoke autounboxing.





  11. (2 pts) Write a line of Java code that invokes autounboxing, but does NOT invoke autoboxing.







  12. (2 pts) Write a line of Java code that declares a variable y that is NOT of a primitive type.






     

  13. (2 pts) Suppose Dog is a class that represents a Dog. Declare a variable called kennel that can store more than one reference to a Dog object. Choose a type for kennel so that it can automatically grow as needed if the number of Dog objects grows arbitrarily large.

    (Assume any necessary import statements are already in place at the top of your program.)





End of Exam

Total points: ?