CSCE 146 Spring 2001

FIRST MIDTERM EXAM

Tuesday 01/02/20

Chapter 1: The Phases of Software Development

Short Answer---three points each

  1. What is a precondition? What is a postcondition? 

A precondition is a condition that is supposed to be true when a method is called.  A postcondition is a statement that described what is true when a method call is completed.

  1. Which tool can provide nicely formatted documentation about how a method works?

javadoc

  1. Give a concise formula that gives the approximate number of digits in a positive integer. The integer is written in base 10.

Floor(log n) + 1

Multiple Choice---one point each

  1. Why is writing easily modifiable code important?

B

  1. Answer true or false for this statement: When programming in teams, the specification of a method must be written by the person writing the code for the method.

FALSE

  1. If the precondition fails, it is a good idea to throw an exception that will generally halt the program. Why is the program halted?

B

  1. Which of these function calls will cause an exception to be thrown when x is 42. (x is an int variable).
o        
o          if (0 < x)
o             throw new IllegalArgumentException("Bad x");
   
o          if (0 == x)
o             throw new IllegalArgumentException("Bad x");
   
o          if (0 > x)
o             throw new IllegalArgumentException("Bad x");
   

 

A

  1. What does a run-time analysis usually count?

A

  1. Which of these is the correct closed-form expression for 1+2+3+...+n?

B

  1. Why is it important to test boundary values when testing programs?

C

 

Chapter 2: Abstract Data Types and Java Classes

Short Answer---three points each

  1. Here is part of the Throttle declaration:
 
    public class Throttle
    {
       public Throttle(int size)
       ...
        
       public double getFlow()
       ...        
       
       public void shift(int amount)
       ...
    }

Write several lines of Java code to create a Throttle called quiz with 100 positions, activate the method that shifts quiz's flow to the halfway point, and then print the current flow from quiz.

Answer: Throttle quiz = new Throttle(100); quiz.shift(50); System.out.println(quiz.getflow());

  1. Part of the Throttle implementation from Chapter 2 is shown below. Mark each method method as follows: Mark C for any constructor; mark M for any modification method. Mark A for any accessor method.
 
    public class Throttle
    {
       public Throttle(int size)... C
       
       public double getFlow( )... A
        
       public boolean isOn( )... A
        
       public void shift(int amount)... M
        
       public void shutOff( )... M
    }
  1. Here is a method with a parameter named spot, which is a reference to a Location object:
   public static foo(Location spot)
   {
      spot.shift(2,0);
   }

Now, suppose that s is a reference to a Location with s.getX( ) equal to 40. Then the method foo(s) is activated. What is the value of s.getX( ) after this activation?

42

  1. Here is a method with a parameter named spot, which is an integer:
   public static foo(int spot)
   {
      spot += 2;
   }

Now, suppose that s is an integer with a value of 40. Then the method foo(s) is activated. What is the value of s after this activation?

40

  1. Suppose I implement a class with a clone method. How should I modify the class head to inform the Java compiler that you plan to implement the clone method.

Add; implements cloneable

Multiple Choice---one point each

  1. Suppose I create two Throttles using the class from Chapter 2:
 
    Throttle mower = new Throttle(...);
    Throttle copter = new Throttle(...);

Which statement best describes the instance variable called top for these two Throttles:

A

  1. Is it possible for a method of a class to activate another method of the same class?

D

  1. What is the common pattern of class definitions that is used in Chapter 2?

C

  1. What is the primary purpose of a constructor?

C

  1. Suppose that the Foo class does not have a clone method. What happens when an assignment x=y; is given for two Foo objects?

B

  1. Suppose that the Foo class has a clone method. What typically happens when an call x=(Foo) y.clone( ); is given for two Foo objects?

A

  1. Consider this class definition:
 
    public class Quiz
    {
       private double score;
       
       public int f( )...
       public static int g( )...
    }

Where could the assignment score=1.0; appear for the private instance variable score?

B

  1. Here is a small method implementation, using the Location type from Chapter 2:
 
    public static void f(int i, Location k)
    {    
        i += 1;         
        k.shift(2, 0);
    }                      

Suppose that a main program has an integer variables a (equal to zero), and a Location object b (with b.getX( ) equal to zero). Then the main program calls f(a,b); What are the values of a and b.getX( ) after the method f finishes?

C

  1. Suppose that the Foo class does not have an equals method. What happens when an expression x==y; is evaluated for two Foo objects?

A

  1. Suppose that the Foo class has a typical equals method. What happens when an expression x.equals(y); is evaluated for two Foo objects?

B

 

Chapter 3: Collection Classes

Short Answer---three points each

  1. What happens if you call new but the heap is out of memory?

The exception OutOfMemoryError is raised.

  1. Suppose that you are storing a collection of items in a partially-filled array. You will declare the array with some initial size. What else must you keep track of?

The number of elements in the collection.

  1. Consider the Bag from Chapter 3 (using an array). What happens if the insert method is activated, and the array is already full? Does the method work even if the array had a weird length of zero?

The capacity is approximately doubled.  Yes: the capacity is set to one.

  1. Suppose that I have the following declarations:
    int[ ] data = new int[100];
    int i;

Write a small segment of Java code that will shift data[50]...data[98] up one spot to the locations data[51]...data[99]. Then insert the number 42 at location data[50]. Use a for loop (not System.arraycopy).

For (I = 98; I > 49; I--) data[I+1] = data[I];  data[50] = 42;

  1. Suppose that I have the following declarations:
    int[ ] data = new int[100];
    int i;

Write a small segment of Java code that will shift data[51]...data[99] down one spot to the locations data[50]...data[98]. The value of data[99] remains at its original value. Use a for loop (not System.arraycopy).

For (I =51; I < 99; I++) data[I-1] = data[I];

Multiple Choice---one point each

  1. Suppose I have int b = new int[42]. What are the highest and lowest legal indexes for b?

A

  1. Who needs to know about the invariant of an ADT?

A

  1. Suppose that the Bag class is efficiently implemented with an array with a capacity of 4000, as in Chapter 3 of the class notes. Choose the best description of b's instance variables after we execute these statements:
    Bag b;
    b.add(5);
    b.add(4);
    b.add(6);

What will be the values of b.manyItems and b.data after the statements?

B

  1. I have an array named data, which contains n integers. I want to print all of the numbers, starting at data[0]. BUT if the number 42 occurs, then I want to stop my printing just before the 42 (without printing the 42!) Here is most of my for-loop to accomplish my goal:
    for (i = 0;  ___________________________________________; i++)
       System.out.println(data[i]);

What is the correct way to fill in the blank? (If there is more than one correct answer, please select E.)

C

  1. In Chapter 3, there was a suggested implementation of the Sequence class with these private member variables:
    class Sequence
    {
        int[ ] data;
        int manyItems;
        int currentIndex;
    };

The Sequence's constructor creates a new array for data to refer to, but does not place any values in the data array. Why?

D

  1. Suppose the Bag and Sequence are both implemented with an array as in Chapter 3. What is the difference between the private instance variables of the Bag and the private instance variables of the Sequence?

D

  1. What kind of object is created to wait for actions such as a button click?

A

 

Chapter 4: Linked Lists

Short Answer---three points each

  1. What is the single instruction needed to insert a new item (say, newData) at the head of a linked list of IntNodes?

Head = new IntNode(newData, head).

Multiple Choice---one point each

  1. Suppose cursor refers to a node in a linked list (using the IntNode class with instance variables called data and link). What statement changes cursor so that it refers to the next node?

D

  1. Suppose cursor refers to a node in a linked list (using the Node class with instance variables called data and link). What boolean expression will be true when cursor refers to the tail node of the list?

B

  1. Which boolean expression indicates whether the numbers in two IntNodes (p and q) are the same? Assume that neither p nor q is null.

B

  1. Suppose that p is a reference variable that contains the null reference. What happens at runtime if the program tries to activate a method of p?

C