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:
  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:
  3. (12 points) If int x = 100, what is the value of each of the following expressions?
    x / 30  
    x % 30  
    x == 99 + 1 ? 5 : 7  
    x++  
    ~x  
    ~(-x)  
    x << 2  
    x >> 2  
    x <<< 2  
    x >>> 2  
    x & x  
    x ^ x  
  4. (2 points) Inside what kind of statement or statements may a break statement be used?


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


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



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


  8. (2 points) What is the primary purpose of the assert statement?


  9. (2 points) When is it better to say class MyClass extends ThatClass {...} rather than class MyClass { ThatClass that; ...} ?


  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
    
    
    
    
    // Write the header line for a public class that extends JFrame
              
              
        // Declare and define a Swing button, a Swing text field, and
        // an int to count clicks
    	
    
    
    
        // Write the header line for your main method
    	
                  
            // Create a new instance of this class, and call its createGui() method
    
    
     
        } // end main
                  
        // Write the header line for the createGui() method
    	
                  
            // Get this JFrame's content pane, and assign it to a variable
                      
                      
            // Set a layout manager
    		
                      
            // Add the text field and button to the GUI
    
    
    
            // Attach a listener of type "Listen" to the button
    		
                      
            // Pack this GUI and make it visible
                      
                      
        } // end createGui
                  
        // Declare an inner class "Listen" which implements ActionListener
                 
                 
            // Declare the required method for an ActionListener
                      
                      
                // Add one to the click count and display the result
    					  
    
    
            } // 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?



  2. (1 point) The method defined on line _____ overrides the method defined on line _____.

  3. (1 point) The method defined on line _____ overloads the method defined on line _____.

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

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


  6. (1 point) What effect does the keyword final on line 9 have?



  7. (12 points) At line 20, what would be printed for:
    s1.ace() s1.twice(3) s1.number()
    s2.ace() s2.twice(3) s2.number()
    s1.foo s1.dubble(6) s1.dubble("8")
    s2.foo s2.dubble(7) s2.dubble("9")


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



  10. (5 points) Name (but do not describe) five JUnit methods other than assertEquals, and specify what type of arguments each method expects.




  11. (2 points) Briefly define the term "factory method."




  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) {...