CSE143 Summer Quarter
University of Washington
Midterm #1
July 11, 2003 

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

 
1 pt. each
Vocabulary check!  On each one, answer A for True (correct use of terminology), B for False (incorrect use of terminology).

. ___ A constructor creates a class

. ___ A constructor creates an object

. ___ A constructor implements an instance

. ___ A constructor implements an object

. ___ A constructor defines a class


B.  

A. A constructor creates an object

B. 

B.

B.

 
1 pt. each
interface ITest {

...

}

which of the following (taken individually) are legal inside the curly braces?  Answer A for legal, B for illegal.

. ___ public ITest( ); //a constructor

. ___ ITest someMethod( );

. ___ void someMethod2(ITest param);

. ___ int someMethod3(int param) { }


B. Interfaces can't define constructors

A.

A.

B. Interfaces can't have an implementation, even one with no statements!

 
1 pt. each
Answer A for True, B for False.

The value of a comment in JavaDoc format is:

. ___ Java programmers are familiar with the format and expect to see it

. ___ The compiler can check that what the JavaDoc comment says is true in the code

. ___ A tool exists which can process the comments to produce HTML


A. 

B. Compiler ignores comments

A. "javadoc" is the tool

 
1 pt. each
Given that concrete class SRule implements IRule, and that these two consecutive lines occur in a correct program:

(local variables)

   IRule i;

   SRule s;

Which of the following lines, taken in isolation, could legally be the next line in the program?  (Assume, if you need to, that classes have public default constructors.)

Answer A for legal, B for illegal

. ___ i = new IRule();

. ___ i = new SRule();

. ___ s = new IRule();

. ___ s= new SRule();

. ___ i = s;

. ___ s = i;


These must be local variables, as none of the statements would be legal following instance variable declarations [announced during the test].

B. can't instantiate an interface

A.

B.

A.

B. s is uninitialized

B. not only is i uninitialized, but there is a type problem

 
.
"package" in Java refers to...

A. a grouping of classes into a directory

B. what you have to do to an int value in order to insert it in an ArrayList

C. the process of putting common methods in an interface

A. A only

B. B only

C. C only

D. A and B only

E. A, B, and C


A only
 
.
Which of the following successfully places the integer value 6 into an existing ArrayList L?

A. L.add(6);

B. L.insert(6);

C. L.add(new Integer(6));

D. L[0] = 6;

A. A only

B. B only

C. C only

D. D only

E. More than one of A, B, C, or E


C only
 
Abstract class Land has an implementation of toString; Farm extends Land and also implements toString; Ranch extends Land but does not have an implementation of toString.

Land myLand = new Ranch(); //line 1

System.out.println(myLand); //line 2

Farm myFarm = new Farm(); //line 3

String fstring = myFarm.toString(); //line 4

 

. ___ On line 2, assuming line 1 is correct...

A. toString of Land is called

B. toString of Object is called

C. there will be a compile error since myLand is not a String

. ___ On line 3 4, assuming lines 1 and 3 are correct (and ignoring line 2 in case it was incorrect)...

A. toString of Land is called

B. toString of Farm is called

C. toString of Object is called


A.  The compiler, looking at line 2, doesn't know that myLand is anything other than a "Land"

B. Farm [the question wording, specifying line 4, was corrected as the test was given].

 
1 pt. each
Answer A for True, B for False

. ___ "catch" is a general term used when discussing error handling, not a Java keyword

. ___ the term "invariant" refers to the use of the "final" keyword in Java.

. ___ The term "invariant" refers to the use of the "static" keyword in Java.

. ___ Generally, at any particular point of a program, there is only a small number of possible invariants that be stated.


B.

B. 

B.

B. There is generally an infinite number of invariants (most of them trival or uninteresting)

 
Consider this method:
    public static void aTest() {
        System.out.print("A");
        assert 1 != 2;
        System.out.print("B");
        if (3 == 4) {
            assert 3 == 4;
            System.out.print("C");
        }
        System.out.print("D");
        assert 5 == 6;
        System.out.print("E");
    }
     

. ___ If asserts are enabled, what does the method print?

. ___ If asserts are not enabled, what does the method print?

A.  A

B.  AB

C. ABD

D. ABDE

E. ABCDE


enabled: ABD

disabled: ABDE

 
. Worth 2 M.C. questions
The specification for a problem reads, in part: "Each string contains a name, in the format last name, comma, first name.  Any string containing a comma is considered legal.  Everything up to but not including the first comma in the string, trimmed for spaces, is considered to be the last name; everything following that comma, trimmed, is considered to be the last first name."

Interpret this specification, and show what the first and last names would be for each of the following strings (probably typed in by a crazed user).  Show spaces as the the _ character.  Indicate if the original string is not legal.

original string first name last name
Lamm,  Mrs. Mary    
Wei Xian    
110 Main St. #4, Defiance, OH    
Last name: Smith, first name: John    

original string first name last name
Lamm,  Mrs. Mary Mrs._Mary Lamm
Wei Xian illegal -- no comma  
110 Main St. #4, Defiance, OH 110_Main_St._#4 Defiance,_OH

 

Last name: Smith, first name: John Last_name:_Smith first_name:_John
In the last row, it's not clear from the formatting how much and what type of whitespace there is between "first" and "name."  Any reasonable answer was accepted.
 
. Worth 8 M.C. questions
A Library Information System keeps track of numerous types of materials (books, CDs, maps, etc.).  In the system, each different type of item is represented by a class which implements ILibraryItem.  Patrons (borrowers) are represented by classes which implement IPatron.

Periodically, the librarian prepares overdue notices (a borrower gets a separate notice for each overdue item.)  Overdue notices are represented by a class called OverdueNotice, which extends the abstract class AOverdueNotice.

Implement the listOverduePatrons method as shown belong, which returns a list of OverdueNotice objects, given the library collection (a list with one element for each item) and the patrons.

Note: additional information is available on the handout.

public static ArrayList listOverduePatrons(ArrayList AllItems, IPatron[] AllPatrons);

 


Full, working code is to be posted separately.   During the test, some students asked if they could assume there was a default constructor for OverdueNotice, and this was answered yes.
          /** Find the overdue items and prepare an overdue notice for each one.
     * @param allItems A list of all Library Items owned by the library.
     * @param allPatrons A list of all patrons who have library cards.
     * @return a list of OverdueNotice objects, one for each item which is
     *   checked out and overdue.
     */
    public ArrayList listOverdue(ArrayList allItems, IPatron[] allPatrons) {
        ArrayList notices = new ArrayList();
        Iterator itemIter = allItems.iterator();
        while (itemIter.hasNext()) {
            ILibraryItem anItem = (ILibraryItem) itemIter.next();
            if (anItem.isOverdue()) {
                AOverdueNotice notice = new OverdueNotice();
                notice.setItemName(anItem.getTitle());
                String pName = anItem.getBorrowerName();
                notice.setPatronName(pName);
                IPatron borrower = findPatron(allPatrons, pName);
                String pAddress;
                if (borrower == null) {
                    pAddress =  "unknown address";
                } else {
                    pAddress = borrower.getAddress();
                }
                notice.setPatronAddress(pAddress);
                notices.add(notice);                
            }
        }
    return notices;
    }
    
    /** Find and return the patron whose name is given.
     *  An exact match is required.
     *  @return the matching patron, or null if there is no match
     */
    private IPatron findPatron(IPatron[] patrons, String patronName) {
        for (int p = 0; p < patrons.length; p++) {
            if (patrons[p].getName().equals(patronName)) {
                return patrons[p];
            }
        }
        return null;
    }