CIT591 Final Exam
Fall, 2004
Name_________________________________________

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

  1. (3 points) What is printed by the following code fragment?
        int n = 2004;
    int i = 0;
    for (i = 0; n > 0; i++, n /= 10);
    System.out.println(i);
    Answer:
    4
  2. (3 points) What is printed by the following code fragment? (Answer has more than one line.)
        String[] strings = new String[] { "A ", "B ", "C "};
        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i]);
            for (int j = 2; j >= 0; --j) {
                System.out.print(strings[j]);
            }
        }
    
    Answer:
    A
    C B A B
    C B A C
    C B A
  3. (12 points) If int x = 100, what is the value of each of the following expressions?
    x / 30 3
    x % 30 10
    x == 99 + 1 ? 5 : 7 5
    x++ 100
    ~x -101
    ~(-x) 99
    x << 2 400
    x >> 2 25
    x <<< 2 illegal
    x >>> 2 25
    x & x 100
    x ^ x 0
  4. (2 points) Inside what kind of statement or statements may a break statement be used?
    Switch, and any kind of loop

  5. (2 points) Inside what kind of statement or statements may a continue statement be used?
    Any kind of loop

  6. (3 points) Name (don't describe) three kinds of layout managers.

    BorderLayout, GridLayout, GridBagLayout, FlowLayout, BoxLayout

  7. (2 points) If you see the statement assert false; in a well-written program, what was the programmer probably trying to indicate?
    "This position in the program can never be reached."

  8. (2 points) What is the primary purpose of the assert statement?
    To document what you believe to be true at that point in the program.

  9. (2 points) When is it better to say class MyClass extends ThatClass {...} rather than class MyClass { ThatClass that; ...} ?
    When objects of MyClass really are a more specific kind of ThatClass.

  10. (28 points) In this question you will write a GUI with a text field and a button. The text field will display the number of times the button has been clicked.

    // Import all the necessary packages
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    // Write the header line for a public class that extends JFrame
    public class Test2 extends JFrame{   
        // Declare and define a Swing button, a Swing text field, and
        // an int to count clicks
        JButton button = new JButton("Click me");
    JTextField text = new JTextField(20);
    int clicks = 0;
    // Write the header line for your main method public static void main(String[] args) { // Create a new instance of this class, and call its createGui() method Test2 test = new Test2(); test.createGui(); } // end main // Write the header line for the createGui() method private void createGui() { // Get this JFrame's content pane, and assign it to a variable Container c = getContentPane(); // Set a layout manager c.setLayout(new GridLayout(2, 1)); // other kinds may be used // Add the text field and button to the GUI c.add(text); c.add(button); // Attach a listener of type "Listen" to the button button.addActionListener(new Listen()); // Pack this GUI and make it visible pack( ); setVisible(true); } // end createGui // Declare an inner class "Listen" which implements ActionListener private class Listen implements ActionListener { // Declare the required method for an ActionListener public void actionPerformed(ActionEvent arg0) { // Add one to the click count and display the result clicks++; text.setText(clicks + " clicks"); } // end method } // end inner class } // end outer class




The questions on this page use the following program:
/*  1 */  interface MyInterface {
/* 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 MainClass {
/* 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 MainClass extends Abs implements MyInterface {
/* 16 */ public int number() { return 11; }
/* 17 */ public static void main(String args[]) {
/* 18 */ MainClass s1 = new MainClass();
/* 19 */ MainClass s2 = new Sub(16);
/* 20 */ System.out.println( );
/* 21 */ }
/* 22 */ int twice(int x) { return 2 * x; }
/* 23 */ int ace() { return 1; }
/* 24 */ String dubble(String s) { return s + s; }
/* 25 */ }
  1. (2 points) In line 11, it is a syntax error to omit the word public. Why?

    When you override a method, you cannot make it less accessible (more private).

  2. (1 point) The method defined on line _____ overrides the method defined on line _____.
    16 overrides 6, or 11 overrides 16, or 12 and 23
  3. (1 point) The method defined on line  13  overloads the method defined on line  24  .
                                                                  (order is not important)

  4. (1 point) To print out foo with its original value of 12, you could put a print statement between lines  17  and  18  .

  5. (2 points) Which of the above classes and/or interfaces have default constructors?
    Abs and MainClass

  6. (1 point) What effect does the keyword final on line 9 have?
    This class cannot be extended.

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

  8. (9 points) True or false:
    1. False A char is the same as a String of length 1.
    2. True Every public (outer) class must be on a separate file.
    3. True Every object has a public String toString() method.
    4. False The declaration String[] s; does not allocate any space.
    5. True The private variables of an object can be accessed by another object of the same class.
    6. True Every class has at least one constructor (including abstract classes).
    7. False The right operand of instanceof must be an object.
    8. False An interface must declare one or more methods.
    9. True An abstract class may contain both abstract and concrete methods.

  9. (2 points) The most commonly used JUnit method is assertEquals(expectedactual). What restrictions, if any, are placed on the two arguments?
    If primitives, they must be of the same type; the first cannot be null.
  10. (5 points) Name (but do not describe) five JUnit methods other than assertEquals, and specify what type of arguments each method expects.
    assertFalse(boolean), assertNull(Object), assertNotNull(Object), assertSame(Object,Object), assertNotSame(Object,Object), assertTrue(boolean), fail()
    All of the above may also have a String as a first parameter.

    (private) failNotEquals(String,Object,Object), failSame(String), failNotSame(String,Object,Object), format(String,Object,Object)
  11. (2 points) Briefly define the term "factory method."
    A static method that calls a private constructor.
  12. (3 points) The following Javadoc comment is badly written. Write a better version, making any plausible assumptions you like about what the method does and how it works.
        /**
    * public String clickDigit(char digit)
    * This method is called when the user clicks a digit key on the fraction
    * calculator. It uses a state machine to decide whether the digit is part
    * of the numerator or part of the denominator.
    */
    public String clickDigit(char digit) {...

    Do not:

    • Repeat information that Javadoc can get from the code (name, parameters, return type). Javadoc will get this right; you might not.
    • Talk about how it is implemented. The user only wants to know what it does.
    • Assume you know who calls it, or why, or what they do with the result.

    Do:

    • Tell what the method does.
    • Use the active voice.
    • Start with a verb ("Computes", "Handles", "Provides", etc.)
    • Use tags (@) to tell what each parameter is for and what the return value is.
    Sample answer:
        /**
         * Handles an input digit.
         * @param digit The digit to be handled.
         * @return A string representing the current state of the calculation.
         */