CIT591 Final Exam
Fall, 2002
  1. (18 points) First, step through the following program, updating the values of all the variables, until you get to the System.out.println statement. Then fill in the following table, telling (1) the simplest possible way to print the variable, and (2) what value will be printed for it. If a variable cannot be accessed, tell why.
    public class Major {
        int a = 1, b = 2, c = 3;
        Extra extra = new Extra();
        public static void main(String args[]) {
            int b = 4, d = 5;
            Minor minor = new Minor(b);
    class Minor extends Major {
        int c = 6, e = 7;
        static int f = 8;
        Minor(int param) { b = e = param; }
        void printAll() {
            System.out.println(   expression from table below  );
    class Extra {
        int f = 9;
        Extra() { f = 10; }
        class Inner { int g = 11; }
        Inner inner = new Inner();
    Variable Simplest expression to access variable Value
    a in Major a 1
    b in Major b 4
    b in main Cannot access local variable outside function
    c in Major super.c 3
    c in Minor c 6
    d in main Cannot access local variable outside function
    e in Minor e 4
    f in Minor f 8
    f in Extra extra.f 10
    g in Inner extra.inner.g 11

  1. (20 points) True or false:
    1. FALSE A char is just a String of length 1.
    2. FALSE Methods may have a maximum of 32 parameters.
    3. TRUE   An abstract class cannot be instantiated.
    4. TRUE   Methods declared in an interface are automatically public.
    5. TRUE   Constants may be declared in an interface.
    6. FALSE The size of an array is part of its type.
    7. TRUE   In BlueJ, a striped box means the class is not compiled.
    8. TRUE   A RuntimeException need not be caught.
    9. TRUE   You can attach more than one Listener to a Component.
    10. TRUE   You can attach a Listener to more than one Component.
    11. FALSE In an if test, zero counts as false, other integers as true.
    12. TRUE   You can do integer arithmetic with a char variable.
    13. FALSE The break statement can only be used within a loop.
    14. TRUE   The continue statement can only be used within a loop.
    15. FALSE Applets can only be run from within a browser.
    16. TRUE   To create an applet, you must subclass the Applet class.
    17. FALSE A Java file can legally contain more than one public class.
    18. TRUE   Anonymous inner classes can access the fields of the enclosing class.
    19. FALSE Every method must have at least one return statement.
    20. TRUE   Any number that begins with a zero is considered to be octal.

  2. (10 points) Give brief definitions of the following terms:

    1. glyph
      The printed representation of a character
    2. wrapper class
      A class whose objects contain a single value (usually a primitive)
    3. magic number
      A number in a program with no obvious reason for having that particular value
    4. Swing
      An alternative to the AWT for constructing GUIs
    5. modal dialog
      A dialog that must be dismissed before processing can continue
  3. (1 point) What is the default layout manager for a Panel?    FlowLayout

  4. (1 point) What is the default layout manager for a Frame?    BorderLayout

  5. (2 points) If array field[][] has been declared and defined as a rectangular array, write an expression that returns:

    1. The number of rows (first subscript) in the array.    field.length

    2. The number of columns (second subscript) in the array.    field[0].length

  6. (3 points) Write a boolean method divisible, with two integer parameters, that tests whether its first parameter is divisible by its second parameter. For full credit, keep your method body as short as possible.

         boolean divisible(int x, int y) {
              return x % y == 0;

  7. (2 points) Should your divisible method from the previous question be declared static? Why or why not?

    Yes, because it has nothing to do with any particular object.

  8. (1 point) How do you declare that a variable has a fixed, constant value?
    Use the "final" modifier

  9. (4 points) What are the four conditions for serializing an object?

    1. The class must be declared as public

    2. The class must implement Serializable

    3. The class must have a no-argument constructor

    4. All fields of the class must be serializable (or transient)

  10. (3 points) Give the name of the Java regular expression method that:

    1. Tries to match the entire string    matches

    2. Tries to match at the beginning of the string.    lookingAt

    3. Searches the string for a match    find

  11. (3 points) Write a regular expression that will match an integer (the integer may be signed or unsigned).

         [+-]?\d+      or     [+-]?[0-9]+

  12. (1 point) How do you prevent a method from being overridden by a method in a subclass?

    Mark it as final

  13. (2 points) What is special about the first sentence of a Javadoc comment?

    It is used as the summary sentence

  14. (1 point) What part of speech (grammatical category) should be used when naming methods?

  15. (1 point) When do the style rules say you should not give a variable a meaningful name?

    When it is a "throwaway" variable, such as the index of a loop

  16. (3 points) Here is a constructor for a class named Point:
         Point(float x, float y) { this.x = x; this.y = y; }
    Write a second constructor for Point that takes one parameter x and calls the above constructor with 1/2 x and twice x.
         Point(float x) {
              this(x / 2, 2 * x);

 1  interface Inter {
 2      int number();
 3  }
 4  abstract class Abs {
 5      static int foo = 12;
 6      int number() { return 5; }
 7      abstract int ace();
 8  }
 9  final class Sub extends Super {
10      Sub(int bar) { foo = bar; }
11      public int number() { return 10; }
12      int ace() { return 13; }
13      int dubble(int i) { return 2 * i; }
14  }
15  public class Super extends Abs implements Inter {
16      public int number() { return 11; }
17      public static void main(String args[]) { 
18          Super s1 = new Super();
19          Super s2 = new Sub(16);
20          System.out.println(      );
21      }
22      int twice(int x) { return 2 * x; }
23      public int thrice(int x) { return 3 * x; }
24      int ace() { return 1; }
25      String dubble(String s) { return s + s; }
26  }
  1. (2 points) In the above program, does Super correctly implement Inter? Why or why not?

    Yes, because it defines number()

  2. (2 points) In line 11, it is a syntax error to omit the word public. Why?
    Because number() in Sub overrides number() in Super(), and you cannot make
    an overridden function less public.

  3. (1 point) The method defined on line _____ overrides the method defined on line _____.
    24 and 12, or 11 and 16, or 16 and 6 (any one pair)
  4. (1 point) The method defined on line _____ overloads the method defined on line _____.
    13 and 25 (or 25 and 13)
  5. [Question deleted.]

  6. (1 point) To print out foo with its original value of 12, you could put a print statement between lines _____ and _____.
    17 and 19 (must be in a method, and must be done before an instance of Sub is created in line 19)
  7. (2 points) Which of the above classes and/or interfaces have default constructors?
    Super (only)
  8. (1 point) What effect does the keyword final on line 9 have?
    Prevents any other class from extending this one

  9. (12 points) At line 20, what would be printed for:
    s1.ace()  1 s2.ace()  13  16  16
    s1.twice(3)  6 s2.twice(3)  6 s1.dubble(6) error s2.dubble(7) error
    s1.number()  11 s2.number()  10 s1.dubble("8")  88 s2.dubble("9")  99