CSE143 Autumn Quarter
University of Washington
Midterm Exam #2
Friday, November 15, 2002 

Hover the mouse over the colored bars to see the answers.  You can also View Source; or save the file, then view source to read the comment near the top which explains how to make the answers always visible.

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


Handouts: Collection Javadoc, Line2D Javadoc
 
.
Recall the definition of big-O, exactly as stated in the lecture slides: Definition: If f(n) and g(n) are two complexity functions, we say that f(n) = O(g(n)) ( pronounced f(n) is O(g(n)) or is order g(n) ) if there is a constant c such that f(n) <= c • g(n) for all sufficiently large n.

Suppose f(n) and g(n) are the exact complexity formulas for two methods, as follows:

f(n) = 3 + n3

g(n) = 10 + 8*n + 10*n2

Which of the following are true?

A. f(2) < g(2)

B .f(10) < g(10)

C. f(n) = O(g(n))

A. A only

B. B only

C. C only

D. A and B only

E. A, B, and C

D.
g(2) = 66, f(2)= 11; f(10) = 1003, g(10) = 1090;  f(n) is not O(g(n)), because n3 is steeper than n2, and before too long becomes greater than it and stays that way to infinity.



 
.
Various programming techniques can be used to find or report various kind of errors.  Choose the best matches.  Among W, X, Y, and Z, use each at most once.
match these errors... with these ways of handling
1. A programming error internal to a method

2. A parameter value which violates a precondition of a method

3. An illegal value in a user data input

W. an assert statement

X. throwing an exception

Y. displaying an error message and prompting for another value

Z. System.exit(0)

A. 1-W, 2-X, 3-Y

B. 1-X, 2-Y, 3-Z

C. 1-X, 2-W, 3-Y

D. 1-Z, 2-X, 3-W

A.  Internal error: assert; bad parameter value; illegal user input: prompt.

You might not always match things this way, especially 3-Y.  But the combination in A is clearly the best.



.
You have written and tested a method, one of whose parameters is of type ArrayList.  You think you can generalize this method by changing the parameter to to List, while making no other changes.  Can you?  Should you?

A. You can do this safely without even looking at the rest of the method code, since ArrayList extends List.  You should do it for sure.

B. This will compile for sure, but it could fail when running some of your previous test cases.  It's still a good idea.

C. It's not guaranteed to work or even compile without modifying the code.  Do it if no changes are needed, otherwise don't.

D. Replacing a parameter type with a superclass is a terrible idea anyway, since it actually makes the method less general rather than more general.  Don't dream of doing it.

C.
Generalization is "generally" a good thing.  Changing the parameter type to a superclass shouldn't affect your clients, but... it won't compile if any of your method code uses subclass-specific messages.   So you need to check it out before going ahead with the change.


 
.
In many GUI component classes, you see this code:

public void paintComponent(Graphics g) {

     g = (Graphics2D) g;

//Note: the above line is in error.

//It was corrected during the test to:

Graphics2D g2 = (Graphics2D) g;

...

The best explanation the cast is:

A.  At run-time, the object referenced by g is modified to become an object of type Graphics2D

B. At run time, the dynamic type of the object referenced by g is actually Graphics2D; the object is not modified

C. This statement simply tells the compiler that Graphics is a sub-class of Graphics2D.

//The answer choices are not affected by the correction

B. 

g and g2 refer to the same object.  The object is in fact a Graphics2D at runtime.  When working this problem, you don't even have to know ahead of time that Graphics2D is a subclass of Graphics.  The fact that this code is common suggests strongly that it is not in error, and that tells you that Graphics2D is a subclass of Graphics.


 
.
If you wish to display a string S on a panel (JPanel), certain steps need to be followed.  Choose an appropriate sequence of steps from the possibilities below (the sequence you select might not be complete, but at least all the steps in it should be appropriate).

1. create a new JPanel [ new JPanel( )]

2. create a new class which extends JPanel

3. create a new Graphics object [Graphics g = new Graphics()]

4. create a new Graphics2D object[Graphics2D g = new Graphics2D()]

5. call paintComponent, giving it the graphics object as an argument

6. call paintComponent, giving it the string S as an argument

7. In paintComponent, use System.out to display the string on the panel

8. In paintComponent, use a Graphics (or Graphics2D) method to display the string on the panel.

A. 1 -- 3 -- 5 -- 8

B. 2 -- 8

C. 2 -- 4 -- 5 -- 7

D. 2 -- 4 -- 6 -- 8

E. 1 -- 2 -- 3 -- 6

B

To override the paintComponent method, you have to make a subclass of JPanel.  Ordinary application code never calls paintComponent directly.  There are cases where an application might create a new Graphics or Graphics2D object, but we haven't seen any of them, and in any case you couldn't pass that object to paintComponent as an argument.

 
 
.
Here is part of a method from MyClass and a bit of  code which invokes it:
static A method1(B arg1) {
...
     return new C();
}
D val1 = method1(new D());

Using the terminology "X is a Y" to mean either "X extends Y" or "X implements Y", what is true?  (Assume the code compiles and runs without error).

A. C is a A

B. A is a D

 C. D is a B

A. A only

B. B only

C. C only

D. A and C only

E. All of A, B, and C

E.

From the method1 declaration and return statement, C is an A. From the invocation argument list, D is a B.  From the declaration of val1 and the fact that it receives the result of the call, A is a D.


 
.
lineList contains a List of all the line segments (edges) from a valid state map file (with counties, as in Project 3bc).

Assume that the following code fragment compiles without errors and executes without failure:

Set S = new HashSet();
Iterator lineIter = lineList.iterator();
while (lineIter.hasNext()) {
     Line2D thisLine = (Line2D.Double) lineIter.next();
     S.add(thisLine.getP1());
     S.add(thisLine.getP2());
}
Iterator sIter = S.iterator();
while (sIter.hasNext()) {
     ........ sVal = (........)  sIter.next();
     System.out.println(sVal);
}
What pair of words could fill in the ....... ?

A. Set / Set

B. Set / HashSet

C. Point2D / Point2D

D. Line2D / Line2D

E. Line2D.Double / Line2D.Double

C.   Point2D / Point2D
The only objects put into the set are acquired from this.Line.getP1() and getP2(), and those are Point2Ds (see handout).


 
.
Referring to the previous problem:

Suppose you know further that the map file contains only 1 county (i.e., is a state map only), and that all segments have length > 0.  Let lCount = lineList.size() and sCount = S.size().  What is the relationship between lCount and sCount?

A. lCount == sCount

B. lCount == sCount/2

C. lCount < sCount/2

D. lCount == sCount * 2

E. lCount > sCount * 2

A.   lCount == sCount. 
From geometry and from the problem: Every line segment has two different endpoints.  Each endpoint is shared by exactly two line segments.  From Java: for each segment, "add" is called to insert two different points into the set.  From the definition of a set (either in mathematics or in Java): Since a set discards duplicates, there is effectively one unique point per line segment.  



 
.
An eccentric professor claims that the keyword "static" is overloaded in Java.   Under this theory (or even without it!), what is true about "static"? A. static applied to a method means that the method may not change the value of any variable it declares.

B. static and final cannot be used together

C. static applied to an instance variable means that it cannot be changed (after initialization)

D. static applied to an instance variable has the same effect as public.

E. None of the above

E.  None of the above


 
.
Suppose class C has an instance variable v which is marked protected.  What is true?

A. The variable v can be referenced from any method of C or any subclass of C.

B. The variable v can be referenced from any protected or private method of classes which extend C, but not from their public methods.

C. The variable v can be read (viewed) by any class, but can be written (changed) only by its own class, C.

A. A only

B. B only

C. C only

D. A and C only

E. B and C only

A.


 
(1 pt. each)

The static method CountPositives is informally described is follows: "Given a list of Integers, count the number of positive values in the List, and return that count.  A null reference is treated like an empty list."

After analyzing the problem (but not seeing the code), a programmer drafted the following assertions.  Mark each one as A, B, C, or D.  Don't forget to bubble in the answers on your Scantron!

A: Precondition

B. Post condition

C. Loop invariant

D. Can't determine without seeing the code

PRE

  A

POST

B

LOOP

C

?

D

. The return value is greater than -2        
.If 0 <= pos and pos is less than the length of the list, then the element at position pos of the list has type Integer.        
.The return value is <= the number of elements in the list.        
.If the first element of the list is positive, the return value is >=1        
.Any variable used within any loop of CountPositives will never have a negative value.        

 

 

B (post);  A (pre);  B (post);  B (post);  D (can't say)

With reference to the first four: The question was not about whether these assertions were valid or not, but how they should be categorized.  In fact, they were all valid.  One person during the exam indicated he thought the first one was was not valid -- but in fact it is!

With reference to the second part: It could be argued that this is a data invariant.  But that was not one of the choices, and besides, we don't enough to state that as a fact.  It might be argued that this is a postcondition as well as a precondition; that would be a reasonable design for such a method.  But it's presented in the informal problem description as a precondition fairly explicitly, and not so as a postcondition, so "precondition" is the best answer.

With reference to the last part: without looking at the code for CountPositives, it is impossible to say what internal invariants exist.

 
 
. Worth 1 M.C. question
Part of software testing and debugging involves coming up with good test cases.  Extreme or unusual cases are especially good at revealing bugs.

True or False: Referring to Project 3: If M is the number of counties in a valid state/county map, then there are at least M segments on the (state) border.

If you answer True, explain carefully why the statement is true.

If you answer False, then give a concrete example (i.e., draw a map) of a case where the statement is false.

 

 

 

 

 

False.  A simple triangular county could have lots of interior, "landlocked" counties.

 
 
. (worth 4 MC questions)
A linked list implementation uses these definitions for MyLinkedList and Link (same as in the lecture slides):
    Public class MyLinkedList implements List {
         private Link first;
         public MyLinkedList( ) {        
              this.first = null;
        }
     }

    Public class Link {
        public Object item;
        public Link next;
        public Link(Object theData, Link nextLink) {
              item = theData;
              next = nextLink;
        }       
    } //end Link
    

Implement a "deep clone" method for such a list.  That is, the method should return a list which has identical data values as the original list, in the same order, but doesn't share ANY objects in common with the original.  You may assume that the items being stored have good clone methods.  You may not assume that any of the usual List methods (except for the constructors shown) already exist (i.e., don't call them!)  Don't change or add to the definitions above.  Indicate what class your method belongs to.

//During the test the following additional clarifications were made:

1. The deep clone is in fact a method of MyLinkedList

2. If you wish, you can change the Link "first" variable from private to public.

Code will follow as a separate file.