H212 Introduction to Software Systems, Honors

    Midterm Exam

    You have 75 minutes. There are 100 points total. You may use your any printed reference material you choose, but not a computer or anyone's help. Check your work thoroughly. Good luck!
     
    1. (6 points) Fix the errors in the following program with the least number of changes.

    2. Class Fooey {
          public static main(String[] gooey) {
              $total = 5 + 2
              System.out.println($total)
          }
      }
       
    3. (6 points) What does the following method program print?
      static void baz() {

    4.     int i = 8, j = 3;
          System.out.println(i/j+","+i%j+","+(i+j*2));
      }
       
    5. (8 points) Write a method named between that takes three integers and returns the one whose value is between the other two (or equal to the high or low value if two are the same).

    6. (15 points) Fill in the MISSING CODE A, B, and C.

    7. abstract class List {
          static List empty = new EmptyList();

          static void test() {
              // could easily generate a long list of literal values this way
              Object[] ab = {"a", "b"};
              System.out.println(List.fromArray(ab)); // prints (a . (b . ()))
          }

          abstract boolean isEmpty();

          List cons(Object obj) { return new Cons(obj, this); }

          /**
           * @return a list containing the elements of array in the same order.
           */
          static List fromArray(Object[] array) {
              // MISSING CODE A
          }
          /**
           * @return a list of the elements of enum in the same order.
           */
          static List fromEnumeration(Enumeration enum) {
              // MISSING CODE B
          }
          /**
           * @return a list of the elements of vec in the same order.
           */
          static List fromVector(Vector vec) {
              // MISSING CODE C, one line only
          }
      }
      class EmptyList extends List {
          boolean isEmpty() { return true; }

          public String toString() { return "()"; }
      }
      class Cons extends List {
          Object head;
          List tail;

          Cons(Object head, List tail) {
              this.head = head;
              this.tail = tail;
          }

          boolean isEmpty() { return false; }

          public String toString() {
              return "(" + head + " . " + tail + ")";
          }
      }
       

    8. (15 points) Indicate changes required to add a length() method (which of course returns the length of a list) to the List class. Use an object-oriented style.

    9.  
    10. (15 points) If method M1 overrides method M2, the protection modifiers that may be associated with M1 are constrained by the protection of M2.  Explain this constraint fully and justify it with a specific example of something going wrong without this restriction.

    11. (15 points) Let Foo be some existing class. Define a class SingletonFoo that inherits from Foo and supplies the additional method singleton(), which takes no arguments and returns an instance of SingletonFoo that is guaranteed to be the only instance of SingletonFoo that can ever be created. (This is a general solution of the Singleton design pattern.) Hint: method protection modifiers may be associated with constructors.

    12. (20 points) Fill in the MISSING CODE.

    13.        /**
           * @param ints is sorted and has length > 0
           * @return the number, N, of distinct values in ints and modifies ints
           * such that ints[0],...,ints[N-1] contain the distinct elements of ints.
           */
          static int removeDuplicates(int[] ints) {
              // MISSING CODE
          }
     
     
     
       
       

Chris Haynes / chaynes@indiana.edu