CMPSCI 187: Programming With Data Structures

David Mix Barrington

Fall, 2011

Practice Exam for First Midterm 

This exam is intended to be the same length, format, and difficulty as the actual first midterm to be taken on Thursday evening 29 September 2011.  There are 100 total points -- the scale of the actual exam will be determined after it is graded.

Section 1: Java Concepts (20)

Briefly explain the difference between the two concepts in each pair (2  pts each):

a) int and Integer

b) & and &&

c) static field (or attribute) and instance field (or attribute)

d) char and char [ ] 

e) abstract data type and data structure

f) return value and side effect

g) throw and throws

h) Stack<T> (in java.util) and StackADT<T> (in L&C)

i) object and class

j) .equals (for Strings) and == (for Strings)

Section 2: Software Engineering (10)

Briefly discuss how you would make the following modifications to the code

for the specified program, with specific reference to the code (5 pts each):

a) In Project 1, to display Maze objects with row 0 at the bottom rather than the top

b) In Project 2, to search only for paths of five or fewer steps

Sections 3 through 6 use the following class:

    public class Dog

       private String name;

       private int age;

       public Dog (String newName, int newAge) {

             name = newName;

             age = newAge;}

       public String getName {return name;}

       public void setName (String newName) {name = newName;}

       public int getAge {return age;}

       public void setAge (int newAge) {age = newAge;}

For all code blocks in Sections 4 and 5 that do not declare new methods, 

assume that the following code is run before the block, in a Dog static method:

   Stack<Dog> s = new Stack<Dog>( );

   Dog [ ] pack = new Dog[3];

   Dog ace= new Dog("Ace", 6);

   Dog biscuit = new Dog ("Biscuit", 1);

   Dog cardie = new Dog ("Cardie", 3);

Assume that each new block starts after this code, without any of the other 

blocks first.

Section 3: Trace Code (15)

Determine the output value of the following methods (5 each)

(a) pack[0] = ace;

      pack[1] = cardie;

      pack[2] = ace;


      System.out.println(pack[0].getAge( ));

(b) s.push(cardie);

       biscuit = s.peek( );


       cardie = s.pop( );

       System.out.println(s.pop( ).getName( ));

( c)  s.push(cardie);



       while (s.peek( ).getAge( ) != 3)

            s.pop( );

       System.out.println (s.pop( ).getName( ));

Section 4: Find Errors (15)

Each of the following code fragments has a specific error that either prevents it

from compiling, will cause an exception if it is run, or produces a clearly 

unintended output.  Find the error and explain what will happen (5  each)

(a) // a new method for the Dog class

      public int dogyears ( ) {

            int age;

            return age * 7;}

(b) for (int i = 0; i < 3; i++)

          pack[i] = new Dog("dog #" + i, i);

      for (int j = 0; j <= pack.length; j++)

          System.out.println (pack[j[.getName( ));

( c) pack[0] = "Ace";

       pack[1] = "Cardie";

       pack[2] = "Biscuit";

Section 5:  Timing Analysis (10)

Find the big-O running time of the following methods, as a function of n, the length of the input array (5 each)

(a) public void replace (Dog [ ] kennel) {

           int n = kennel.length;

           for (int i = 0; i < n; i++) {

               kennel[i] = new Dog("Cardie", 3);

               for (int j = 0; (j < 7) && (j < i); j++) 


(b) One solution to Discussion #2 was as follows, in pseudocode:

         // move all n containers from the ship to the left stack

         // while the left stack is not empty

         //    shift all containers from the left stack to the right, finding the lowest label

         //    shift all containers from the right stack to the left, but sending out the 

         //          first container that has that label instead of shifting it


Section 6: Short Coding Problem (10)

Write the specified classes or methods (10 each)

a) Write a method that will take a Stack<Dog> object s as its parameter and alter it as follows.  If s is empty or has just one element, make it empty.  If it has two or more, leave it with two, and these should be the original bottom two, in reverse order.  For example, if the elements were Ace, Biscuit, and Cardie (starting from the top), the stack should have Cardie and Biscuit (starting from the top) after the method is run.

Section 7: Long Coding Problem (20)

Write the specified classes with all specified methods (20 points)

b) Write classes JeopardyBoard, Category, and Question, where a JeopardyBoard object contains six Categories and each Category has a name (a String) and contains five Questions.  A Question has a text (a String),

a dollar value (an int), and a boolean telling whether it is available.  Write a method

for the Category class that returns the first available Question that

it contains, or returns null if it has no available Question.  Then write a method for

the JeopardyBoard class that takes the number of a Category as its parameter,

and returns the first available Question in that Category, or return null if there is none.

Last modified 23 September 2011