15-100 Sections S-V / Fall 2008
Quiz 8 / 15 Questions / 30 Minutes
Quiz Date:  Tue 18-Nov-2008

  1. Write a single line of code total, and not one line per part, that demonstrates all of the following, each of which must be clearly labeled:
    1. Assignment Conversion
    2. Method Invocation Conversion
    3. Promotion
    4. Casting
       
  2. \b is the escape sequence for _______________________.
     
  3. The smallest byte value is ______________ and the largest char value is _________________.
     
  4. In the code below, “a” is a(n) _______________ parameter and “b” is a(n) _______________ parameter.
       public int foo(int a) {
         int b = a – 32;
         return Math.abs(b);
       }
     
  5. Give an example of each of the following in Java’s libraries (and not your own classes):
    1. A static method
    2. An instance method
    3. An overloaded method
    4. A static field
       
  1. The following code demonstrates overflow.  What, exactly, will it print?
    SHOW YOUR WORK!
        byte b1 = 76;
        byte b2 = 100;
        byte b3 = (byte) (b1 + b2);
        System.out.println(b3);

     
  2. Write one line of code that shuffles the values in an array “a” of Strings.
     
  3. Unlike arrays, ArrayLists are _____________________________.
     
  4. Unlike ArrayLists, HashSets are not _______________ and do not contain ____________________.
     
  5. Wrapper classes allow the JCF to work with __________________________.
     
  6. In just a few words of plain English, what does the following method do?
      public static int g(ArrayList<Integer> list) {
        return (new HashSet<Integer>(list)).size();
      }

     
  7. Given the following code, what will the call   f(4, 3, -1, 6, -3, -2, 17)   print?
      public static void f(int... a) {
        int sum = 0;
        for (int i : a)
          if (i % 2 == 0)
            sum += i;
          else
            System.out.print((i < 0) ? i : ""); // note: not println
        System.out.println(sum);
      }

     
  8. What will the following code print?
        int x = 0, y = 10;
        while (true) {
          System.out.println((x++) + "," + y); // note x++
          if (x < 3)
            continue;
          else if (--y < 9) // note --y
            break;
        }
        System.out.println(x + "," + y);

     
  9. What will the following code print?
        int x = 0, y = 7;
        for ( ; x<=y; x++,y-- ) // note unusual usage
          switch (x) {
            case 0:
            case 2: System.out.print("A");
                    break;
            case 3: System.out.print("B");
                    x++;
                    // note: no break!  fall-through!
            case 1: System.out.print("C");
                    y++;
                    break;
            default:
                    System.out.print("D");
          }
        System.out.println(x + "," + y);

     
  10. Write the following method (which may be a helper method from Tetris):  This method takes a 2d array of booleans, the “piece”, and returns another 2d array of booleans representing that same piece rotated 90 degrees counterclockwise.  Do not worry about changing the piece’s left or top to keep its center fixed, just create the new 2d array.

    public boolean[][] rotate(boolean[][] piece)
    {
    }

     
  11. BONUS:  What will the following code print?
        int x = 0, y = 250;
        for ( ; x<=100*y; x+=1,y-=1)
          for ( ; x<=10*y; x+=10,y-=10)
            for ( ; x<=y; x+=100,y-=100)
              ; // do nothing!
        System.out.println(x + "," + y);

     

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