15-110 Sections G-J / Spring 2010 / Midterm #2

24 questions / 70 minutes / 100 points

Note:  Unicode ‘A’ is 65, ‘a’ is 97, and ‘0’ is 48.
Also note:  all code compiles and runs without errors.  And you may use almostEqual without writing it.

Part A:  Short Answers (Be very brief)  [3pts each]

1.       In our Snake implementation, say our timerFired tries to set the headRow and headCol to a board location that contains the value -1.  From the user’s perspective, what just happened?

2.       In just a few words, how do we represent the 8 possible directions in 2d board search?

3.       When will Java not provide a default constructor for a class you write?

4.       Indicate how to test for equality for each of the following Java types:

a.       Two Strings s1 and s2

b.       Two ints i1 and i2

c.       Two arrays of Strings a1 and a2

d.       Two doubles d1 and d2

5.       Say you wrote a class Foo but instances of Foo print out as something like Foo@4810C2A.  What did you probably forget to do?

6.       List two good reasons for using method overloading.

7.       Why do we use mutators rather than directly modify instance variables?

8.       In just a few words, why doesn’t the compiler require you to initialize instance variables before using them (even though it does require you to initialize local variables before using them!)?

9.       In just a few words, what does “this” mean in Java?

10.   Strings are immutable in Java.  Very briefly, what does this mean?

11.   What must be true in order for binary search to work properly over an array?

12.   Say you have a large sorted array, and you choose a random location and swap two adjacent elements.  You now have an unsorted array (if barely).  Which sort is best for re-sorting the array in this specific situation?

13.   In the xSortLab, what does it mean when a bar changes color to black?

14.   Very briefly, and in English (not Java!), explain how selectionsort works.

15.   Our course web site links to the Java API.  In just a few words, what is the Java API and why do we care?

Part B:  What will the following code snippets print?  [3pts each]

16.     int[] a1 = new int[6], a2 = { 1, 2, 3, 4, 5 };
  for (int i=0; i<a1.length; i++)
    if ((i < a2.length) && (a2[i] % 2 == 0))
      a1[i] = a2[i];


17.     String[][] a = { { "this", "is", "a" },
                   { "2d", "array", "of strings" } };
  for (int i=0; i<a.length; i++)
    for (int j=0; j<a[0].length; j++)
      if (a[i][j].indexOf("s")>=0)
        a[i][j] = i + "-" + j;


18.     String[] a = {"ab", "cdef", "g", "", "hij"};
  boolean didSwap = true;
  while (didSwap) {
    didSwap = false;
    for (int i=1; i<a.length; i++)
      if (a[i].length() > a[i-1].length()) {
        swap(a, i, i-1); // assume swap is already written
        didSwap = true;


19.     String[] a = new String[5];
  a[0] = "Wahoo";
  a[1] = "W" + a[0].substring(1);
  System.out.println(a[0] == a[1]);


20.     int[] a = { 2, 3, 4 };
  System.out.println(foo(a));  // foo defined below

  // where foo is defined as:

  public static String foo(int[] a) {
    for (int i=1; i<a.length; i++)
      a[i] += a[i-1];
    a = new int[] { 5, 6, 7 };
    return Arrays.toString(a);


Part C:  Writing Code  [10pts each]

21.  Write a method named median that takes an array of ints and returns the median value also as an int.  You may not modify the array’s values, but you may call Arrays.sort (on another array).  The median of an array with an even number of elements is the average of the middle two values.

22.   Write a class named Complex that models a complex number.  In particular, write a constructor, a toString, and a times method.
Hint #1:  Define a complex number (a+bi) with two doubles, a (the "real" part) and b (the "imaginary" part).
Hint #2:  (a+bi) * (c+di) = (ac-bd) + (ad+bc)i

23.  Write a Java program that animates a red ball bouncing up-and-down along the right edge of the window.  When the user presses ‘b’, the ball becomes blue (and stays blue from that point on).  You may assume all imports are already included.

24.   Write the fallingPieceIsLegal method from our Tetris implementation.  You may use the instance variables fallingPiece, fallingPieceRow, fallingPieceCol, board, and emptyColor.

Part D:  Bonus/Optional:  [3pts each]

25.   Bonus/Optional:  What will the following code snippet print?
  int[] a = { 2, 3, 4, 15, 25, 35 };
  bar(a,0);  // bar defined below
  // where bar is defined as:
  public static void bar(int[] a, int i) {
    if (a[0] <= a[a.length-1]) {
      a[0] += a[a[i]%a.length];

26.   Bonus/Optional:  Answer the following:
  for (int expt=0; expt<20; expt++) {
    int x = 0;
    for (int i=0; i<(int)Math.pow(2,expt); i++) x ^= i;
Hint:  ^ is the bitwise xor operator.

a.       In the code above, for what values of expt will the code print 1?

b.       In the code above, for all other values of expt, the code prints 0.  Briefly explain why.