CSE143 Winter Quarter
University of Washington
Midterm #1 with answers
Friday, January 31, 2003 

  Closed book, closed notes, closed neighbor; no calculators
2 points per part except as noted

To view an answer, hover the mouse over the colored block


Handouts: Javadoc for String (missing last page: some relevant methods were noted on Corrections transparency)
 
.
As applied to an instance variable, static implies... A. unchanging

B. unchangeable

C. shared among instances

D. accessible only in static methods

E. accessible only in constructors

C
 
.
Consider that the following three statements have just executed (yes, without error!):

Target t1 = new Target();

CenterCircle t2 = new Bullseye();

t1 = t2;

Just after the last statement executes, the static type of t1 is _______ and the dynamic type of t1 is ______.

A. Target ... Target

B. Target ... Bullseye

C. CenterCircle ... Bullseye

D. Bullseye ... CenterCircle

E. Bullseye ... Target

B
 
(1 pt. each)
Note that in the following code, some lines have labels (as comments).  Read the code, then for each question that follows, pick one line ("A" - "D") which illustrates the case mentioned.  Answer "E" if there is no labeled line to illustrate the case (or if the case is impossible).  Don't forget to bubble each answer on your Scantron!

class Super {

    public Super( ) {System.out.println("A");}

    public Super(int i) {System.out.println(i);}      /*line A*/

    public int roll ( ) {return 1 + 5*Math.rand( );}  

}

class Sub extends Super {

    public Sub( ) {             /*line B*/

             super(1);

             System.out.println("B");}  

    public Sub(int i) {         /*line C*/

             super(i);

             System.out.println(i);}

    public int roll (int r) {   /*line D*/

             return 1 + r*Math.rand( );}    

}

.  ____ is an example of a overloading a constructor

.  ____ is an example of an overriding a constructor

.  ____ is an example of overriding a method (other than a constructor)

.  ____ is an example of overloading a method (other than a constructor)

A or B or C; E; E; D
 
.
The closest definition for "stream" (the general concept, not a particular class) in Java is... A. a file

B. a sequence of data

C. an array of bytes

D. a network connection

E. a data source or sink

B
 
.
In general, when a java.io class has Stream in its name, it is treated as a sequence of _______; when a class has Reader in its name, it is treated as _______. A. bytes ... bytes

B. bytes ... chars

C. bits ... strings

D. bytes ... strings

E. output ... input


B
 
.
Assume that the following statements compile without error:

final double pi = 3.14159;

int newvalue = (int) pi;

What happens when the second statement executes?

A. The value of the variable pi is changed from 3.14159 to 3

B. There will be a run-time error, because the variable pi is final

C. The value 3.14159 is temporarily assigned to newvalue, and then changed to 3

D. The variable pi is unchanged and newvalue references that value

E. newvalue gets the value 3

E

A cast never changes the original value.  With a primitive type, it may produce a new variable (perhaps anonymous) with a value of the new type.  So pi is completely unchanged.  newValue gets the value 3.

 
.
If a Java method has a catch block, then the method must also...

A. have a throw statement

B. have a try block

C. have a throws clause

A. A only

B. B only

C. C only

D. more than one of A, B, and C

E. none of A, B, or C

B only
 
. (worth 2 MC questions)

Using whatever hints you can infer from the following Java code, draw a diagram to show any inheritance relationships among the four classes mentioned.  If you can be sure that a class is concrete (as opposed to being an interface or abstract class), label it with "C".

P1 x = new P2();

P3 y = x;

P4 z = (P2) y.getIt();

P1 is a superclass of the concrete class P2.

P3 is a superclass of P1.

P4 is a superclass of P2.

The diagram would show P3 at the top, and P2 at the bottom.  By convention, arrows point from the subclass to the superclass.

 
. (worth 2 M.C. questions)

Sun's Javadoc for the String class says "The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Strings are constant; their values cannot be changed after they are created" (emphasis added).

Consider these two consecutive lines of code in a program:

    String str = "abc";

  str = "uvwxyz";

First answer whether you think these two lines (in a reasonable context in a Java program -- no trick intentions!) ARE or ARE NOT legal.  ("It depends" is not an option.)  Then explain your answer.  Draw a diagram if it helps make your case.


The statements ARE legal.  str = "uvwxyz" does NOT change the value of the string created by str = "abc".  It creates a separate, new String, which str now refers to.  "abc" is unchanged.  Instead of a verbal explanation, a picture in the Official 143 Style would make the point.
 
. (worth 3 M.C. questions)
The two classes shown below have a whole bunch of complicated stuff (AWBOCS)-- duplicated four times.  Modify the classes to eliminate the duplication as much as possible, so that the resulting classes would be exactly equivalent.  ("Exactly equivalent" means regardless of how clients used the classes, and regardless of what was in the duplicated code.)  For full credit, do not define additional methods or constructors.  Write directly on this sheet.
class Upper {
    
    protected String author;
    
    public Upper () {
        this.author = "Charles Dodgson";
        /********** AWBOCS ***********
         * a whole bunch of complicated stuff.*/
    }
    
    public Upper (String auteur) {
        this.author = auteur;
        /********** AWBOCS ***********
         * a whole bunch of complicated stuff -- exactly the same 
         *  as in the other constructor, letter for letter.*/    
    }    
} //end upper question class
        
class Lower extends Upper { //question lower class
    
    public Lower () {
        this.author = "Lewis Carroll";
        /********** AWBOCS ***********
         * a whole bunch of complicated stuff -- exactly the same 
         *  as in the other constructors, letter for letter.*/
    }
    public Lower (String auteur) {
        this.author = auteur;
        /********** AWBOCS ***********
         * a whole bunch of complicated stuff -- exactly the same 
         *  as in the other constructors, letter for letter*/
    }
}
Sample code will be provided.

Common errors: calling this() or super() incorrectly; assigning the author value after calling this or super -- an error, because AWBOCS might refer to the author value.

 
. (worth 4 M.C. questions)
For an alumni directory application, a class FamilyName must be developed. The class will store a last name without leading or trailing spaces, and can also compare two last names to see which should come first in the directory. 

When comparing names, non-letter characters are ignored.  The comparison should be case-insensitive.  For example, "de la Renta" and "Dela Ren-Ta" should compare as equal.  The exception is names beginning with Mc; these names are compared as if they were spelled Mac.  Thus McZebra comes before Madsen.  Aside from these cases, the comparison rules are the normal ones for Strings.

There is exactly one method which you must implement to complete the class, but you may define and implement other private methods if desired.  In your solution, you can use any String method, and the static Project0 method CharGenerator.isUpperCaseEnglish(c),  but no other methods except ones you fully define as part of the solution.

public class FamilyName implements Comparable {

     private String origN;

     public FamilyName(String nameString) {

         this.origN = nameString;

         this.origN.trim();

    }

    public String getName() {return origN;}

    /** Implement the method required */

 

"CompareTo" is the method required by Comparable.

public int compareTo(Object otherName) {

}

Many solutions were possible.  A sample will be posted later.