COMP 110 Fall 2006

Second Midtem Exam

This exam is closed book, closed notes, closed classmates. You have 75 minutes to complete it.

Please put all answers on the test sheets. Make sure your answer is clearly marked and easily distinguished from work you do to get to the solution. You should show your work for partial credit. This especially applies to the program execution simulations. I showed you in class how to do this in a way that will assist you in getting the correct answer, as well as allow partial credit.

When done, please staple together your exams sheets and any paper you use for work and place it face down on the lecture table up front. Please print your name clearly below, and sign when you hand in the exam, signifying your adherence to the honor code and attesting to the work being yours alone.



Name (print CLEARLY) --------------------------------------------------------



Signed  ---------------------------------------------------------------------

  1. (5%) Define: Runnable Java program
    
    
    
    
    
    
    
    
    

  2. (5%) Which of the following is NOT a good programming practice:
    (a) making code as compact as possible to save storage
    (b) choosing variable names so they are meaningful to code readers
    (c) use of indentation to clearly show program structure
    (d) commenting major algorithm sections to explain the functionally
    (e) using named constants rather than having numbers scattered in the code
    
    
    answer: _______________
    
    

  3. (5%) From the following list, select the 3 items that are the main advantages provided by Object Oriented Programming:
    (a) multiple source files per program
    (b) information hiding and encapsulation
    (c) inheritance... class reuse
    (d) separation of compiler and execution engine
    (e) freedom from logical errors
    (f) polymorphism... name overloading
    
    
    answer: _______________
    
    
    
    

  4. (5%) Select the item that best defines "byte code":
    (a) the binary pattern used to represent a byte of computer memory
    (b) the error numbers printed by the compiler when a program is
        syntactically incorrect
    (c) the byte lengths of different numeric types (short, int, long,
        double, float)
    (d) the executable intermediate language output by the javac compiler
    
    
    answer: _______________
    

  5. (5%) Select the item that best defines "strong typing":
    (a) allowing new classes to be written by using fields and methods
        already written in other classes
    (b) requiring that each variable used in a program be named according
        to a naming standard (such as object fields begin with "_",
        class names being with upper case, etc.)
    (c) writing code without looking at the keyboard
    (d) enforcing that only data of the correct declared type is ever
        assigned to each variable
    
    
    answer: _______________
    

  6. (5%) Select the answer that best defines "scope"
    (a) a view of a variable that allows it to take on different data
        types at different times
    (b) the portion of a program text where a variable name is visible
    (c) use of the parameter list to determine which of several methods
        with the same name is the one being called
    (d) variable names must be chosen so they have descriptive value to
        people reading the program
    
    
    answer: _______________
    

  7. (5%) What is the binary representation of the decimal number 201? Show your work or explain your reasoning for partial credit.
    
    
    
    
    
    answer: __________________________
    
    

  8. (10%) What is printed when this Java code fragment is executed?
       final int MAX = 4;
       int num = 1;
       int k = 0;          // loop index
       while (k < MAX) {
          num = num * k;
          k++;
       }
       System.out.println(num);
    
    
    answer: ________________
    
    

  9. (10%) What is printed when this Java code fragment is executed?
       final int MAX = 6;
       int num = 1;
       int k = 1;          // loop index
       while (k < MAX) {
          num = num * k;
          k++;
       }
       System.out.println(num);
    
    
    answer: ________________
    
    

  10. (10%) What is printed when this Java program is executed?
    class DemoConcepts {
    
       int x = 0;
    
       public int foo (int p) {
         x = p;
         int x = 5;
         return x;
       }
    
       public static void main (String[] args) {
         int p;
         p = foo(12);
         System.out.println("x: " + x+", p: "+p);
       }
    }
    
    
    answer: ________________
    
    

  11. (10%) What is printed when the following Java code fragment is executed?
       int x = 5;
       int f = 5;
    
       if (x < 3) { x = x + 1; }
       else if (x == 5) { x = x - 2 ; }
       else if (x < 0) { x = x * 3; }
       else { x = -8 ; }
    
       if (f < 3) { f = f + 1; }
       if (f == 5) { f = f - 2 ; }
       if (f < 0) { f = f * 3; }
       else { f = -8 ; }
    
       System.out.println("x: " + x + ", f: " + f);
    
    
    answer: ________________
    
    
    
    
    
    

  12. (10%) What is printed when this Java program is executed?
    public class NumCrunch {
       public static int num = 99;
       int k = 0;
    
       NumCrunch() { k = num; num++; }
    
       public int getK () { return k; }
       public int getN () { return num; }
    
       public double crunch (double num) {
          return num * num;
       }
    }
    
    public class SomeThing {
    
       public static void main (String[] args) {
          NumCrunch nc1 = new NumCrunch();
          NumCrunch nc2 = new NumCrunch();
          NumCrunch nc3 = new NumCrunch();
          NumCrunch nc4 = new NumCrunch();
    
          nc2.crunch(5);
          nc3.crunch(8);
          System.out.println( nc2.getK()+",  "+nc1.getN() );
       }
    }
    
    
    answer: ________________
    
    

  13. (15%) What is printed when this Java program is executed?
    class DemoStuff {
    
       public static void main (String[] args) {
         final int MAX = 8;
         int[] info;
         info = new int[MAX];
         for (int i=0; i<MAX; i++) {
            info[i] = 1;
            for (int k=1; k<=i; k++) {
               info[i] = info[i] * k;
            }
         }
         System.out.println(info[4]);
       }
    }
    
    
    answer: ________________