CIT591 Final Exam
Fall, 2003

Please keep all your answers short and to the point. Read the questions carefully and answer only the question that was asked--do not provide extra information that was not asked for.

When I ask for a "single statement," that means one statement that does not contain any other statements.

In your Java code, be sure I can tell the difference between your lowercase letters and your CAPITAL LETTERS.

  1. (25 points) Short answer:
    1. What is the default layout manager for an Applet?     FlowLayout

    2. What is the default layout manager for a Frame?     BorderLayout

    3. What type of parameter does the applet paint(...) method take?     Graphics

    4. What type of value is 5L ?     long

    5. A hex (hexadecimal) digit is equivalent to how many bits?     4

    6. What class is like an array that gets bigger when you add things to it?     Vector

    7. What are the two kinds of polymorphism?     overriding and overloading

    8. When will the expression x % y give a negative result?     When x < 0 and y is not a divisor of x

    9. What operation does the ^ operator (as in, x ^ y) perform?     Exclusive or

    10. Another term for "static variable" is      class variable .

    11. What color is returned by the call new Color(0, 0, 255)?     blue

    12. When you declare an object (for example, String s;), what is space allocated for?     A reference to a String

    13. What is the immediate superclass of Error?     Throwable

    14. On what stream does printStackTrace() put its output?     System.err

    15. What package contains the Random class?     java.util

    16. What package contains the Observer interface?     java.util

    17. What package contains the ActionListener interface?     java.awt.event

    18. What character encoding does Java use?     Unicode

    19. The Java API documentation was generated by what program?     javadoc

    20. In order to run an Applet, you need to create what additional file (besides the .class files) ?     An HTML file

    21. Name an AWT Component that is also a Container.     Panel (or: ScrollPane or Window)

    22. What does the acronym UML stand for?     Unified Modeling Language

    23. What should not change when you refactor a program?     functionality

    24. A method that is used to create objects is called a   factory   method.

    25. Name one keyword that is reserved but not used by Java.     goto or const

  2. (3 points) Variable ch is a char variable, containing a letter in the first part of the alphabet. Write a single statement to change ch to be the next consecutive character (for example, if ch has value 'e', your statement will change it to have value 'f').
         ch++;   or   ++ch;    or   ch = (char)(ch + 1);    but not   ch = ch + 1;
  3. (3 points) Write a single statement that will print out 5 lines if the value of lineCount is 5, but will print out 1 line if the value of lineCount is 1.
       System.out.println(lineCount + (lineCount == 1 ? " line" : " lines"));
    or   System.out.println(lineCount + " line" + (lineCount == 1 ? "" : "s"));
  4. (3 points) Write a single statement to set integer x to the numeric value of s, where s is a String that contains only digits.
          x = Integer.parseInt(s);
    or  x = new Integer(s).intValue();

  5. (4 points) Write nested for loops to print out all the values in the array defined by int[][] rag = { {1}, {2, 3}, {4, 5, 6} }; Each row should be printed on a separate line.
       for (int i = 0; i < rag.length; i++) {
         for (int j = 0; j < rag[i].length; j++) {
           System.out.print(rag[i][j] + " ");
  6. (3 points) What is printed by the following program?
  7. public class CallTest {
    	public static void main(String[] args) {
    	    int n = 5;
    	    int[] a = new int[] {1, 2, 3};
    	    modify(n, a);
    	    System.out.println(n + " " + a[0] + a[1] + a[2]);
    	static void modify(int x, int[] y) {
    	    y[1] = x;
    }            5 163
  8. (10 points) Each of the following statements is legal in some context (you do not need to specify the context in which the statement is legal). Tell briefly what each statement does. Do not use the keywords this or super in your explanation (for example, for that=this; don't say "assigns this to that").

    1. that = this;
      Assigns the current object to variable that.

    2. this();
      Calls the no-argument constructor from within another constructor.

    3. super();
      Calls the superclass no-argument constructor from within a constructor.

    4. super.that();
      Calls the that() method defined in the superclass of the current object.

    5. that(this);
      Calls the that method with the current object as a parameter.

  9. (8 points) What lines are printed out by the following program? (Be sure to write the lines in the correct order.)
    public class Test {
        String instVar;
        static String statVar = "A";
        Test() {
            System.out.println("test: " + instVar + statVar);
            instVar = "B"; statVar = "C";
        public static void main(String[] args) {
            Test t1 = new Test();
            t1.instVar = "D";
            t1.statVar = "E";
            Test t2 = new Test();
            System.out.println("inst: " + t1.instVar + t2.instVar);
            System.out.println("stat: " + t1.statVar + t2.statVar);
    test: nullA
    test: nullE
    inst: DB
    stat: CC
  10. (3 points) What will happen when the following code is executed?
         int i = 5;
         while (i > 0);
         System.out.println("Blast off!");
    It will go into an infinite loop.

  11. (2 points) Every method declared within an interface is automatically both public and abstract (two keywords).

  12. (2 points) Elegant software is both simple and powerful.

  13. (2 points) If we construct an object of type Foo, where
    class Foo { int x = 5; int y; Foo() { x = 2; } }
    what are the values of x and y in the new object?
         x = 2, y = 0

  14. (2 points) Briefly describe the two arguments to the instanceof operator.
    Left argument is an object, right argument is the name of a class or interface.

  15. (3 points) Write a single statement equivalent to x %= y; that does not use the %= operator.
    x = x % y;

  16. (2 points) Under what circumstances does the expression x ^ y return true?
    When x and y have different (boolean) values.

  17. (4 points) If class Foo implements Runnable and contains a run() method, write the code necessary to start an object of this class running in a new thread.

       Thread t = new Thread(new Foo());

  18. (4 points) If vec is a Vector and x is an int, both the following statements are wrong. Correct them.

    1. vec.add(x);
      vec.add(new Integer(x));

    2. x = vec[0];
      x = (Integer)(vec.elementAt(0)).intValue();

  19. (2 points) Briefly, tell how you can "break" a constructor chain.
    By adding a constructor to a class with subclasses that depend on the default constructor being present.

  20. (5 points) Write a method that, given a nearly-but-not-quite sorted int array, counts how many elements are "out of place." An element is "out of place" if it is larger than the element that follows it.

    int outOfPlace(int[ ] array) {
       int count = 0;
       for (int i = 0; i < array.length - 1; i++) {
          if (array[i] > array[i + 1]) count++;
       return count;

  21. (1 point) Name one method that is inherited by every object:
    toString(); also clone(), equals(Object), finalize(), getClass(), hashCode(), notify(), notifyAll(), wait(), wait(long), and wait(long, int).

  22. (1 point) A method whose use is discouraged (because it has been replaced by a newer, better method) is called a deprecated method.

  23. (1 point) A class used to create an object from a primitive, so that the primitive can be used where an object is required, is called a(n) wrapper class.

  24. (1 point) You can cause the current Thread to stop doing anything for five seconds by writing the statement Thread.sleep(5000);

  25. (1 point) You don't need a browser in order to use an Applet; you can use the standalone program appletviewer.

  26. (1 point) In a static method it is illegal to use the keyword this.

  27. (4 points) Which of the following are objects in Java? (Write "yes" or "no" for each.)

    1. arrays   yes

    2. errors   yes

    3. events   yes

    4. exceptions   yes