CIT591 Midterm Exam
Fall, 2002
Name_________________________________________

Please keep all your answers short and to the point. Do not provide extra information that was not asked for.

  1. (45 points) For each of the following, write the simplest means of referring to the given variable or method, or "NO" if it cannot be accessed from that context. If you can use either the name of a class or the name of an object, use the name of the class.

    public class A {
        static int staticA;
        int instanceA;
        private int privateA;
    
    
        void methodA() {
            int localA = 1;
            A a = new A();
            B b = new B();
            C c = new C();
            // Inside methodA
    
        }
    }
    
    public class B extends A {
        static int staticB;
        int instanceB;
        private int privateB;
    
    
        void methodB() {
            int localB = 2;
            A a = new A();
            B b = new B();
            C c = new C();
            // Inside methodB
    
        }
    }
    
    public class C {
        static int staticC;
        int instanceC;
        private int privateC;
    
    
        static void methodSC() { 
            int localC = 3;
            A a = new A();
            B b = new B();
            C c = new C();
            // Inside methodSC 
    
        }
    }
    
    Inside methodA Inside methodB Inside methodSC
    staticA      
    instanceA      
    privateA      
    localA      
    methodA      
    staticB      
    instanceB      
    privateB      
    localB      
    methodB      
    staticC      
    instanceC      
    privateC      
    localC      
    methodSC      
  2. (2 points) What is printed by the following code?

       for (int i = 4; i < 10; i += 2) System.out.print(i + " ");


  3. (3 points) In words (don't write code), how can you make an instance variable "read only," so that it can be seen but not changed by methods in other classes?





  4. (8 points) The following method has four syntax errors in it. (Assume that a GroceryItem really does have a public name and price). Find all four errors and fix them.
        public double computeTotal(GroceryItem[] item) {
            double total = 0;
         itemNumber = 0;
            while (item[itemNumber] !== null) { 
                total += item[itemNumber].price;
                System.out.println(item[itemNumber].name + "  " +
                                   item[itemNumber].price);
                itemNumber = itemNumber + 1
            }
            System.out.println("TOTAL     " + total);
        }   
    
    
    
    
    
  5. (12 points) In a very few words, tell what each of the following keywords means or does.

    1. static


    2. break


    3. this

       
    4. void


    5. instanceof


    6. final



  6. (4 points) In the method call g.drawRect(10, 20, 30, 40), what does each of four parameters indicate?

    1. 10

    2. 20

    3. 30

    4. 40

  7. (5 points) This is a question about naming conventions. Suppose you want to form a name from the words open book. How would you write this as the name of:

    1. A class

    2. An object

    3. A variable

    4. A method

    5. A constant

  8. (14 points) Briefly define each of the following terms:

    1. elegant (as applied to software)


    2. declare (a variable)


    3. define (a variable)


    4. soft tab


    5. formal parameter


    6. signature (of a method)


    7. call by value


  9. (8 points) Tell what words each of the following acronyms stands for:

    1. JRE


    2. AWT


    3. GUI


    4. IDE


  10. (4 points) Write a constructor for the following class that sets both x and y:
    class Point2D {
        double x, y;
    
    
    
    
    
    
    
    
    
    
    
    }
  11. (4 points) Assuming a correct solution for the previous question, write a constructor for the following class that sets all of x, y, and z. Do not write any unnecessary statements.
    class Point3D extends Point2D {
        double z;
    
    
    
    
    
    
    
    
    
    
    
    
    } 
    



  12. (8 points) For each of the following expressions, assume that r, x, y, and z are integer variables with values x=1, y=2, z=3 in each expression, and tell what value is assigned to r. Or, if there is a syntax error, tell what it is.

    1. r = ++x + y--;

    2. r = z % y;

    3. r = (int)(y / (double z));

    4. r = z << z;

    5. r = y ^ z;

    6. r = (x < y ? --z : x = z);

    7. r = (z % 2 == 1 ? (z << 2) - z + 1 : z >> 1);

    8. r = x = y = z;


  13. (4 points) For each of the following expressions, assume that p is a boolean variable, m is an integer variable with the value m=5. Tell what value is assigned to p. Or, if there is a syntax error, tell what it is.

    1. p = 0 < m < 10;

    2. p &= m > 10;

    3. p = (0 < m) & (m + 8 > 10);

    4. p = p ^ p;


  14. (3 points) Suppose you have the following class:
        class Silly { int number = 5;
                      void bump(Silly s, int num) { s.number++; num++; }
        }

    and in a method in some other class you execute these statements:
        Silly s = new Silly(); Silly t = new Silly(); int n = 8;
        s.bump(t, n);
        System.out.println(s.number + " " + t.number + " " + n);

    What gets printed?







  15. (6 points) True or false:

    1. Java inserts a run-time check for every cast operation.

    2. A change in one class may cause a syntax error in a different class.

    3. A subclass can access private variables in its superclass.

    4. Two or more names may refer to the same object.

    5. Static methods can only use static variables, not instance variables.

    6. Local variables have "package" visibility unless declared otherwise.


  16. (5 points) Write the number "three" as:

    1. A decimal integer

    2. An octal integer

    3. A hexadecimal integer

    4. A float

    5. A double