CIT591 Midterm Exam
Fall, 2002

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 staticA staticA A.staticA
    instanceA instanceA, a.instanceA, b.instancea instanceA, a.instanceA, b.instancea a.instanceA, b.instancea
    privateA privateA, a.privateA, ((A)b).instanceA  NO  NO
    localA localA  NO  NO
    methodA methodA(), a.methodA(),b.methodA() methodA(), a.methodA(),b.methodA() a.methodA(), b.methodA()
    staticB B.staticB staticB B.staticB
    instanceB b.instanceB instanceB b.instanceB
    privateB  NO privateB  NO
    localB  NO localB  NO
    methodB b.methodB() methodB(), b.methodB() b.methodB()
    staticC C.staticC C.staticC staticC
    instanceC c.instanceC c.instanceC c.instanceC
    privateC  NO  NO NO, c.privateC
    localC  NO  NO localC
    methodSC C.methodSC() C.methodSC() methodSC()
    Notes for question 1: In each case, the first answer is the expected answer. Where more than one answer is given, the additional answers refer to different variables having the same name but belonging to the locally created objects. Knowing how to refer to variables is essential to programming in Java; if you missed more than a very few, this is the most important thing for you to study.
  2. (2 points) What is printed by the following code?

       for (int i = 4; i < 10; i += 2) System.out.print(i + " ");
          4 6 8
  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?
    Make the variable private and provide a getter method but not a setter method.
    Yes, making it final does make it read only by methods in other classes, but it also makes it read only in the class in which it is defined!
  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;
        int itemNumber = 0;
            while (item[itemNumber] !== null) {  // !== should be !=
                total += item[itemNumber].price;
                System.out.println(item[itemNumber].name + "  " +
                itemNumber = itemNumber + 1 ;
            System.out.println("TOTAL     " + total);
            // either return a number (preferably total) or
            // change return type to void
  5. (12 points) In a very few words, tell what each of the following keywords means or does.

    1. static
      Associated with the class itself or just one of it

    2. break
      Get out of a loop or a switch statement

    3. this
      The object executing the method

    4. void
      Says a method does not return anything

    5. instanceof
      obj instanceof Class tests whether the object belongs to the class

    6. final
      Constant, or cannot be changed

  6. (4 points) In the method call g.drawRect(10, 20, 30, 40), what does each of four parameters indicate?
    (A properly labeled picture is OK)
    1. 10
      x, or how far from the left edge

    2. 20
      y, or how far from the top

    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)
      Both simple and powerful

    2. declare (a variable)
      Tell what type the variable has

    3. define (a variable)
      Allocate space for the variable (preferred answer) or
      Give the variable a value
      (acceptable answer)

    4. soft tab
      Pressing the tab key actually inserts spaces rather than a tab character
      A "tab" is actually a character; a soft tab is a tab emulated by spaces.
    5. formal parameter
      A parameter named in the definition of a method

    6. signature (of a method)
      That which distinguishes it from other methods (correct answer), or
      The method name and the number and types of its parameters (Java only)
      I didn't grade too hard on this one, Java, the method name is part of the signature,
      and the return type is not. Please remember this.
    7. call by value
      Values of actual parameters are copied into the formal parameters

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

    1. JRE
      Java Runtime Environment

    2. AWT
      Abstract Windowing Toolkit (or anything close to this)

    3. GUI
      Graphical User Interface

    4. IDE
      Integrated/Interactive Development Environment
      IDE properly stands for Integrated Development Environment, but I've
      used "Interactive" in a couple of places, so I'm accepting that answer as well.

  10. (4 points) Write a constructor for the following class that sets both x and y:
    class Point2D {
        double x, y;
        Point2D(double x, double y) { //OK to use other names
            this.x = x;  // 1 point off if set to constants
            this.y = 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;
        Point3D(double z) {  // OK to use other names
            super(x, y);     // Must be present, must be first
            z.this = 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--;
      4     (2 + 2)
    2. r = z % y;
      1     (3 modulo 2)
    3. r = (int)(y / (double z));
      Syntax error; double cast should be in parentheses
    4. r = z << z;
      24    (3 * 23)
    5. r = y ^ z;
      1    (0010 xor 0011 = 0001)
    6. r = (x < y ? --z : x = z);
      Syntax error; low precedence x=z should be in parentheses
    7. r = (z % 2 == 1 ? (z << 2) - z + 1 : z >> 1);
      10    (true, so 3 * 22 - 3 + 1 = 12 - 3 + 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;
      0 < m is true, true < 10 is an error
    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?
    5  6  8

  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
      3f or 3.0f
    5. A double