Exam 1: MidtermSolutions and Commentary
Part of
the homework for CS:2820, Spring 2016
|
X X X X X Mean = 6.11 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X ___________X___X_X_X_X_X_X_X_X_X_X_X_X_X_____ 0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10
X X X X X X X X X X X X Mean = 7.78 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X ___________________X___X_X_X_X_X_X_X_X_X_X_X_ 0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10
X X X X X X Mean = 11.00 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X _________________X___X___X___X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_____ 0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10. 11. 12. 13. 14. 15
X X X X X X X Mean = 24.80 X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X X _______________________X_____X___X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X___ 0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32 Midterm Grade Scale | D | C | B | A |
i = 1010; j = 1000; while (i != j) { System.out.println( ""+i ); i = i - 1; }
A question: Why does this only iterate 10 times if i and j are declared as int but it goes into an infinite loop if they are declared to be of class Integer? (1 point)
__the comparison i!=j compares handles (memory addresses), not values__ _______________________________________________________________________ _______________________________________________________________________
Over half got this right. 1/6 earned partial credit for sensible but wrong observations.
__these are different, distinguished by different parameter classes____ _______________________________________________________________________ _______________________________________________________________________
5/6 got this right, and 1/10 had vague answers worth partial credit.
__No, y() is only called if x() returns false__________________________ _______________________________________________________________________ _______________________________________________________________________
5/6 got this right, and 1/10 earned partial credit.
public class IllegalNameException extends Exception {} public Intersection( Scanner sc, LinkedListinters ) throws IllegalNameException { // scan and process one intersection name = sc.next(); if (RoadNetwork.findIntersection( name ) != null) { Errors.warning( this.toString() + " redefined." ); throw new IllegalNameException(); } String skip = sc.nextLine(); // Bug: What if more junk at end of line (skip not empty) }
a) When initializeNetwork() calls the initializer Intersection(sc,inters), it must be inside a try-catch block. What is the name of the exception that should be used on the catch clause? (1 point)
__Intersection.IllegalNameException____________________________________
Nobody got full credit by qualifying the exception name with the class name, but over 1/2 earned decent partial credit. Shockingly, almost 1/2 gave a wide range of painfully wrong suggestions. The most popular was NoSuchElementException (1/10), followed by FileNotFoundException (1/12).
b) Why define a new subclass of exception? Why not just use a generic throw new Exception(). (1 point)
__Doing this, the catch block would catch any exceptions thrown________ __by the scanner or findIntersection()_________________________________ _______________________________________________________________________
1/5 did well. Over 1/2 earned partial credit, frequently with platitudes that had no clear connection to the problem. Many who earned no credit wrote about easier to interpret error messages for the user, but by catching all exceptions here, there will be no error messages, and when a user other than the programmer sees an uncaught exception, it is almost never helpful!
c) Consider this code Errors.warning(this.toString()+" redefined.");. Why must you be careful in calling this.toString() from within an initializer? (1 point)
__Initialization of fields toString() uses may not be complete_________ _______________________________________________________________________ _______________________________________________________________________
1/4 got this right, 1/2 earned partial credit, frequently spoiling a decent answer with the false assertion that there is no this during initialization (see the next question for additional insight into this issue).
d) Consider what happens when the initializer throws the exception. It has already called this.toString(), so the new object, this must already exist. What becomes of this object after the exception is thrown? (1 point)
__It is lost or discarded (it will be reclaimed by the garbage_________ __collector on its next sweep through memory)__________________________ _______________________________________________________________________
Just under 1/2 did well. There was little partial credit, and wrong answers were very difficult to classify but frequently seemed to hinge on the mistaken idea that the object does not exist until after the initializer returns. The question itself already tried to clarify this issue, but apparently unsuccessfully.
Errors.warning(this.toString()+" redefined.");
Recall that this.toString() concatenates the string "Intersection " with this.name.
a) How many times, at minimum, is each character in the string "Intersection " copied in order to call Errors.warning()"? (1 point)
__2 times, once inside this.toString(), once in the given code_________
About 1/2 did well. Almost as many earned varying degrees of partial credit. Strangely, 2 students (who earned no credit) said 12 times.
There are 2 clearly documented concatenations in the question, and each must involve creating a new object into which the characters of both operands are copied. Therefore, each individual character must be copied at least twice. (More copies may be done by non optimal implementations of Java.) Some students added copies for creating the quoted string constants, but these copies are done just once before the program runs, so they do not count in questions about the run-time behavior of the code. Some students went on to describe internal behavior of Errors.warning() but this goes into material that was not given here so is not relevant.
b) How many new objects are created (at minimum) for parameter passing in carrying out this call to Errors.warning()? (1 point)
__Two -- the two strings created by the two concatenations_____________
The score distribution was similar to that for the previous problem, but strangely, some students who got the previous problem right were confused here.
A question: This seems awkward. Why not use the == comparison operator? (1 point)
__the comparison i==j compares handles (object identity), not values___ _______________________________________________________________________ _______________________________________________________________________
Almost 3/4 got this right, and almost 1/4 earned partial credit.
This and problem 1 were essentially the same question, but here, it was asked in the familiar context of strings, as discussed in class, instead of the alien context of class Integer. Bad wording and confusing explanations were the source of most partial credit, but several students confused object type (generally a synonym for class) with object identity, and others seemed very confused about the complexity of the comparison, suggesting that one or the other comparison operator involved a search through the set of all objects (this is not the case).