15-100 Sections S-V / Fall 2008 / Midterm Exam #2
Exam Date:  Fri 5-Dec-2008
5 Parts (20 points each) / 50 Minutes

Note:  Unicode 'A' is 65, 'a' is 97, and '0' is 48.

Part I:  Short Answers

1.       Circle the sort(s) that only compare elements that are contiguous (next to each other).

   Insertionsort        Selectionsort         Bubblesort         Mergesort          None of these

2.      
Circle the sort(s) that is/are most efficient if the array is already sorted:

   Insertionsort        Selectionsort         Bubblesort         Mergesort

3.      Sort the array { 4, 2, 3, 1 } by mergesort.  Just list the array after each entire pass.

4.      Given the array { 0, 2, 4, 6, 8, 10, 12, 14 }, list the values, in order, that binary search would check in order to confirm that the value 7 does not occur in the array.

5.      The following code demonstrates overflow.  What, exactly, will it print?

SHOW YOUR WORK!
    byte b1 = 65;
    byte b2 = 71;
    byte b3 = (byte) (b1 + b2);
    System.out.println(b3);


Part II:  More Short Answers


6.       The ____________________ interface is used to define the natural ordering of some class.

7.       The ____________________ interface is used to define an unnatural ordering of some class.

8.       The ____________________ interface is used to indicate that some collection of objects can be used by an enhanced for (
"foreach") statement.

For the following questions, assume we have a collection of Student instances with some obvious fields.

9.       Among JCF classes, the ___________________ class is best used to find the hometowns, without duplicates, among all the Students.

10.   Among JCF classes, the ___________________ class is best used to find a Student instance given his or her andrew ID.

Part III:  Understanding code.

11.  
What will this code print?

int x = 10, y = 2;
do {
  System.out.println(x + "," + y);
  if (--x == 9)
    continue;
  switch (x % 3) {
    case 1: x--; break;
    case 2: x--; // no break!
    default: y++;
  }
} while (x > y);
System.out.println(x + "," + y);
 

12.   What will this code print?
Hint:  It does not run forever.

public static void bar() {
  byte b = 0;
  while (b >= 0)
    b++;
  System.out.println(b);
}

 

13.   In general, and in just a few words of plain English, what does this method do?

public static int g(double d) {
  return Integer.parseInt(String.format("%.0f",d));
}
 

Part IV:  Write the following method.
 

14.   Write a method that takes a 2d array of int's and returns a 1d array containing the smallest value from each row.
For example, given this array:
  {  { 4, 2, 8, 7 },
     { 3, 9, 1, 4 },
     { 5, 5, 5, 5 }
  }

Your method would return this array:
  {  2, 1, 5 }
 

Part V:  Write the following class.
 

15.Write the Cow class so that the following code passes all its tests.  Do not worry about any test cases besides those given here.

  private static void testCowClass() {
    System.out.print("Running Cow class tests...  ");

    // Constructor, toString, accessor, and mutator
    Cow cow = new Cow("Fred", 10);
    assert(cow.toString().equals("Cow(name=Fred, age=10)"));
    assert(cow.getName().equals("Fred"));
    cow.setName("Ginger");
    assert(cow.toString().equals("Cow(name=Ginger, age=10)"));

    // Another constructor   
    cow = new Cow();
    assert(cow.toString().equals("Cow(name=Gilligan, age=2)"));

    // Natural ordering
    Cow cow1 = new Cow("Ann", 8);
    Cow cow2 = new Cow("Dan", 5);
    Cow cow3 = new Cow("Betty", 5);
    Cow cow4 = new Cow("Zeb", 3);
    Cow[] cows = { cow1, cow2, cow3, cow4 };
    Arrays.sort(cows); // sort using Cow's natural ordering, which is
                       // by age, youngest first, ties decided alphabetically
    assert(cows[0].getName().equals("Zeb"));
    assert(cows[1].getName().equals("Betty"));
    assert(cows[2].getName().equals("Dan"));
    assert(cows[3].getName().equals("Ann"));
    System.out.println("Passed all tests!!!");
  }
 

Part VI:  Bonus
 

16.  Bonus/Optional:  what will this code print?

String[] a = "c3%d4@3e2".split("@");
System.out.println(String.format(a[0],(int)Double.parseDouble(a[1])));
 

17.  Bonus/Optional:  what will this code print?

int a = 35, x = ('A' + a + "a").length()*a/2-1;
for (int i=20; i>-30; i-=20)
  System.out.print((char)(x+((i>=0) ? i : (34+i))));
 


carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem