CSE143 Spring Quarter
University of Washington
Midterm #2
May 16, 2003

  Closed book, closed notes, closed neighbor; no calculators
2 points per part except as noted

 
(1 pt. each)
Assume this line of code is part of a bug-free program;  pList is of type ArrayList:

Paper p1 = (Paper) pList.get(iNum);

 

 

Answer each question with A for TRUE, B for False.

. T/F: this statement causes a copy of the object pList.get(iNum) to be made and assigned to p1.

. T/F: this statement does not create a copy of the object pList.get(iNum), but it does change that object from whatever it was into a Paper object.

. T/F: the cast (Paper) has no effect on the object pList.get(iNum); it is just a promise that pList.get(iNum) will actually refer to a Paper object at run time.

. T/F: since p1 is already of type Paper, the cast (Paper) is actually unnecessary.

F; F; T; F

The .get method does not make a copy -- it just returns a reference to the existing object.  A cast of an object does not in anyway change that object -- it is simply a promise that the programmer makes, that at run time the object's type will be as stated.

 
Study the method below, and then answer the questions about it.  You may assume that this method, and the class which contains it, compiles without error.  randomDouble is a method which returns a different random double value every time it is called. ("random" means that one value is as likely to occur as any other value.)

 

  public ArrayList randL (int m) {
    ArrayList rList = new ArrayList();
    for (int count = 0 ; count < m; count++) {
     while (rList.size() < count) {
       double rand1 = randomDouble();
       double rand2 = randomDouble();
       if (rand1 <= this.maxLength && rand1 >= this.minLength &&
           rand2 <= this.maxWidth && rand2 >= this.maxWidth) {
       //*** Point X ****
              Paper piece = new Paper(rand1, rand2);
              rList.add(piece);
       }
     }
   }
    //*** Point Y **** 
   return rList; 
//end method  
}

(1 pt. each) Note the points marked X and Y in the method.  At those points, which of the following identifiers are in scope?  Answer A for "yes -- in scope" or B for "no -- not in scope"

. rand1 at X 

. piece at X

. count at X

. rand1 at Y

. this.maxLength at Y

. piece at Y

. count at Y


11. A (rand1 is in scope at X) 

12. B (piece is not yet defined at X)

13. A (count is in scope at X)

14. B (rand1 no longer in scope at Y)

15. A (this.maxlength must be an instance variable, which means it is in scope in any method of the class)

16. B (piece in no longer in scope at Y)

17. B (count is no longer in scope at Y)

 
(Refers to the same randL method as in the previous questions.) . If parameter m is 0, what does the randL method return?

A. null

B. 0

C. an empty list

D. a list whose length cannot be predicted

E. the method will give an exception (error) at run time and not return anything

 

. Assume m > 0.   What can be said about the size of rList just before the method returns?

A. the size == m

B. the size > m

C. the size < m

D. the size is unpredictable, because of the random nature of the inner loop


C.  The returned value is rList, no matter what path is taken through the method.  The list has been declared, and constructed, so it starts out as a non-null list with no elements.  The for-loop body never executes, so no elements are ever added to the list.

C.  We don't know how many times the inner loop will execute each time, due to the random nature of the double values generated.  But it doesn't matter!  The inner loop condition is list.size() < count.  This means that no matter how many times the inner loop is executed, when it finally does exit, list.size() == count.  The list size is not changed anywhere in the program except inside the loop body.   The highest value of count for which the loop body executes is count == m-1.  So the final value of .size() is m-1.

 
(1 pt. each)
Study the following code fragment, which compiles without error and runs as intended:

 

int[ ]  iarr =new int[5];

iarr[0] = -42;

. What is the length of iarr?

A. 0    B. 1    C. 4    D. 5    E. 6

. What is the index of the last element of iarr?

A. 0    B. 1    C. 4    D. 5    E. 6

. What is the type of iarr?

A. int    B. int array   C. ArrayList    D. Integer

. What is the type of iarr[0]?

A. int    B. int array   C. ArrayList    D. Integer

. What is the value of iarr[0]?

A. 0    B. 1    C. -42    D.  uninitialized   E. initialized but unknown

. What is the value of iarr[1]?

A. 0    B. 1    C. -42    D.  uninitialized   E. initialized but unknown


D; C; B; A; C; A

Arrays are fixed at the size for which they are declared.  iarr has size 5; its smallest index 0, and the largest is 4.  iarr is of type "int array" (in a free-response answer you could also say "array of ints" or "int []").  The type of each element, such as iarr[0], is int.  The value of iarr[1] has not been assigned by the programmer in this code fragment, so it still has its default value of 0 (note that non-array local variables are not initialized by default).

 
.
In both Linear Search and Binary Search of a sorted ArrayList, a certain number of the list elements must be probed (have their values checked) in order to determine the return value.

If the ArrayList contains 4000 randomly generated but sorted elements, estimate the number of probes needed by Binary Search to find the answer.  Chose the answer closest to your estimate.

A. 12

B. 40

C. 400

D. 2000

E. 4000


A. After each probe, half of the remaining (unexamined) elements of the ArrayList are "discarded", that is, they never will be considered again.  After probe 1: 2000 elements remain; after probe 2, the number is 1000, then 500, 250, 125, 63, 32, 16, 8, 4, 2, and 1, when the probing stops because the answer is known.   Thus 12 probes are needed.  Small discrepancies in the series (due to rounding, for example) will not change the estimate by enough to change the answer you should choose.
 
. (worth 3 M.C. questions)
ASCII Art!  Write a method which draws a 'Z'-shaped figure, as large as possible within a square of the given dimensions.  As with the homework methods, this one should take a single parameter.  (Most other questions you might have can be answered by thinking about the homework requirements.)

    public void drawZ (int dimensions) {
        for (int line = 0; line < dimensions; line ++) {
            for (int col = 0; col < dimensions; col++ ) {
                if (line == 0 || line == dimensions-1 || 
                                    line == dimensions -1 - col) {
                    System.out.print('*');
                } else
                    System.out.print(' ');
            }
            System.out.println();
        }
    }
A common solution, also perfectly acceptable, is a method with three loops.  Loop1 prints the top line of the Z; loop 2 (which must be a nested loop) prints the diagonal); and loop3 prints the bottom line.
 
Code for this problem includes classes ApartmentBuilding and Apartment.  Study these classes.  There are no intentional errors, although some methods may have comments indicating they are incomplete.  Then work the following problem. 
      /********** class Apartment **********/
 public class Apartment {
    int numBedrooms;
    boolean hasParking;
    double monthlyRent;
    String aptNumber;
    
    public Apartment (int bedrooms, boolean parkingYes, double rent, String aNum) {
        this.numBedrooms = bedrooms;
        this.hasParking = parkingYes;
        this.monthlyRent = rent;
        this.aptNumber = aNum;
    }
    
    public int getBedrooms() { return this.numBedrooms;}
    
    public boolean getParking() { return this.hasParking;}
    
    } //end of Apartment class
    
    /************* class Apartment Building *********/
    
  class ApartmentBuilding {
    String buildingName;
    ArrayList apartments;
    
    public ApartmentBuilding(String bName) {
        this.buildingName = bName;
        this.apartments = new ArrayList();
    }
    
    /*********** COMPLETE THIS METHOD **************/    
    /** Tells how many one-bedroom apartments with parking 
        there are in this building. 
     *  "Tells" means returns the value; no printing is done.
     */
    public int countOneBWithP() {

















    } //end of method countOneBWithP


   } //end class ApartmentBuilding      
      

start a new page
 
. (worth 4 M.C. questions)
Complete the countOneBedWithP method.   Write directly on the page where the method is.  Don't make any other change to the classes.   There will be slightly more credit if your solution uses an Iterator (correctly, of course!).

[start a new page]
   public int countOneBWithP() {
        Iterator aIter = this.apartments.iterator();
        int goodAptCount = 0;
        while (aIter.hasNext()) {
            Apartment anApt = (Apartment) aIter.next();
            if (anApt.getParking() == true  &&
                        anApt.getBedrooms() == 1)
                goodAptCount = goodAptCount + 1;
        }
        return goodAptCount;
    } //end of method countOneBWithP