Exam 1: Midterm

Solutions and Commentary

Part of the homework for CS:2820, Spring 2019
by Douglas W. Jones
THE UNIVERSITY OF IOWA Department of Computer Science

Grade Distributions

Exam I

Mean   = 4.38             X
Median = 4.5              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_X_X_X_X_X_X_X_X_X_X_______
   0 . 1 . 2 . 3 . 4 . 5 . 6 . 7 . 8 . 9 . 10

Machine Prolbems 1 – 2

                                          XX
                                          XX
                                          XX
                                          XX
                                          XX
                                          XX
                                          XX
                                          XX
                                        X XX
                                        X X 
                                    X X X X 
Mean   = 8.55                       X X X X 
Median = 9.2                      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

Homework 1 – 5

                                    X             X
                                    X X       X   X X   X
Mean   =  9.87                      X X       X X X X   X
Median = 10.4                       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

Total Scores

                                                          X
                                                          X
                                          X             X X
                                          X X           X X
Mean   = 22.81                            X X X         X X
Median = 23.0                         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. 34
                        F    |    D    |    C    |    B    |    A    |

It is really too soon to give letter grades, but if the world came to an end today and I was forced to assign grades on the basis of the semester up to now, the curve would look something like the above. Note that 5 students who are still registered in the course who did not take the midterm are not reported in the above curve.

Solutions and Commentary

  1. Background: Consider this code from a solution to MP2:
    for (Gate g: gates) System.out.println( g );
    

    Obviously, this outputs each element of gates, a collection of objects of class Gate, where the collection could be a List.

    a) What method of object g does this code call? (0.6 points)       ____toString________________

    Just over 1/2 got this, there was little partial credit.

    b) What method of object gates does this code call? (0.6 points) ____iterator________________

    Just over 1/4 got full credit. Among those earning partial credit, 1/6 suggested next, a method that applies to iterators, while 1/12 suggesteda some kind of method that allows indexing into the list gates, a much weaker answer.

  2. Background: The code we've been working on in class included awkward code to deal with string handles that could be null, for example:
    if (name != null) return "Intersection " + name;
    return "Intersection ???";
    

    It would be nicer to write something like this:

    return "Intersection " + SOMETHING( name );
    

    a) What should SOMETHING be?
    -- this.aMethod (A (dynamic) method of this class?)
    -- aMethod (A static method of this class?)
    -- someObject.aMethod (A (dynamic) method of some other class?)
    -- SomeClass.aMethod (A static method of some other class?)
    Circle one. (0.6 points)

    I can't circle in HTML, so I used bold face to highlight the best answer, a static method of some other class.

    The method SOMETHING is a helper method that takes any string as a parameter, so it has no obvious connection to this class but can be used anywhere that a string variable that might be null should be replaced with question marks if so.

    The method SOMETHING has no dependency on any object other than the strng passed as a paremter, so it should not be a dynamic method of any class.

    Almost 1/4 got this, while over 1/3 suggested a static method of this class for partial credit.

    b) Finish the code for aMethod: (1.2 points)

    ... various modifiers .. String aMethod( String s ) {
    
    ____if (s != null) return s;___________________________________________
    
    ____return "???"_______________________________________________________
    
    _______________________________________________________________________
    
    _______________________________________________________________________
    
    _______________________________________________________________________
    }
    

    Alomst 1/2 got this, although there were many different ways of writing the code. 1/4 earned no credit, mostly with wildly irrelevant and sometimes long and elaborate of code.

    The following error were made by about 1/12 each:

    • Using name instead of the parameter s.
    • Forgetting the else clause.
    • Using s.equals(null).
    • Adding extra code to a basically sound solution.

  3. Background: In the posted code for the road network problem, the lists of all roads and intersections are part of the main class. As an alternative, we could make the list of roads part of class Road, and the list of intersections part of class Intersection. Consider declaring the list intersections inside class Intersection as a static private member of the class.

    a) What code should add each new intersection to the list? (0.6 points) 

    _____the constructor___________________________________________________
    

    Over 1/4 got this. Over 1/6 earned partial credit.

    Partial credit was given for those who suggested adding an add method to class Intersection since this will work, although it's a bit clunky.

    Partial credit was given to those who wrote code here that did things like intersections.add(this) since this would have the right value inside the constructor, so they were implying a sensible answer.

    b) What else must be added to class Intersection so that the main program can iterate through intersections in order to print them out? (0.6 points) 

    _____something to export an iterator over the list_____________________
    

    Over 1/4 got this.

    Almost 1/4 suggested a method to return an entire copy of the list, for partial credit.

    1/12 suggested providing a method that would iterate over the list, where you would somehow pass it the work to be done and it would iterate that work. This partial credit answer either suggests a misunderstanding of Java iterators or an understanding of lambda expressions.

    Among answers earning no credit, about 1/12 suggested some kind of to-string method.

    c) The Road constructor needs to look up the source and destination roads in the list of intersections. It could do it using the mechanism from part b to iterate through all intersections, but it would be better to move what code into class Intersection. (0.6 points) 

    _____findIntersection__________________________________________________
    

    1/3 got this. A few earned partial credit.

    Among the odder wrong answers, almost 1/6 suggested moving class Road into class Intersection.

    It is possible that this was due to a typo in the question. The phrase "destination roads" should have read "destinations of roads." This seems to have been obvious to most students, whose familiarity with the running example appears to have made them all but blind to the typo.

  4. Background: The test script we have been using for our road network exercise runs the program many many times using very small test files. Here is one of the tests from that script:
    touch testdata
    java RoadNetwork testdata
    rm -f testdata
    

    a) What does the first line of this script do? (0.6 points)

    _____It creates an empty file__________________________________________
    

    Over 1/3 got this. Over 1/3 earned partial credit, mostly for failing to state that the created file is empty.

    Among wrong answers, most verged on nonsense, but several said that it connects, reads, or gets the test file, odd statements implying some kind of content manipulation.

    b) Can this test be combined with other tests involving reading roads and intersections from the input file? Why or why not? (1.0 points)

    _____No, testdata must be empty for this test, so we can't add to it.__
    
    _____Yes, we could append other tests reading other test files.________
    

    Due to a quirk in the wording that I didn't anticipate, it was possible to argue correctly for either a yes or a no answer. The above examples are among the best and tersest of the answers illustrating the two readings of what it means to combine tests.

    Over 1/12 gave the first answer, and over 1/6 gave the second. A further 1/12 earned partial credit by the suggestion that we could run the test again, an idea sort of related but in an unclear way. 2/3 gave wrong answers that were very hard to interpret.

  5. Background: In lecture, we experimented with forcing the scanner to advance to the next line after each command and also skip end-of-line comments starting with --. The following code did not work:
    String ending = sc.nextLine();
    if (!(ending.eq( "" ) !! ending.startsWith( "--" ))) Errors.warn( msg );
    

    a) Why does it give an error on this input? intersection x --comment" (0.6 points)

    _____In this case, ending = " --comment";______________________________
    
    _____The leading blank prevents startsWith from matching.______________
    

    Almost 1/6 got this. Almost 1/4 earned partial credit by saying what ending was not instead of what it was.

    Among those earning no credit, a shocking number seemed to think that the boolean expression was wrong. The negation of the or of two terms was apparently too much.

    A less shocking source of confusion involved the assumption that the nextLine operator returned the entire line, including that part that had already been scanned, or alternatively, that it skipped to the end of line and then returned the whole next line. Either of these assumptions makes the entire code given in the question totally wrong instead of subtly broken. The code was written, discussed and demonstrated in class, where it sort of worked.

    There was a typo, the operator || was mistyped as !! (they are hard for me to distinguish with my old eyes.) All students were notified of this typo as soon as someone brought it to our attention.

    b) This code also gives an error if a line has trailing blanks. Why? (0.6 points)

    _____In this case, ending = " " (or more blanks);______________________
    
    _____A string of 1 or more blanks does not match the null string.______ 
    

    Over 1/4 got this. Almost 1/6 eared partial credit for telling what ending is not instead of what it is.

    Among those earning no credit, a shocking number seemed to think that "" was a string of one blank instead of the empty string.

    c) What do we need to do to fix this problem? (Explain what the code has to do, not how to do it.) (0.6 points)

    _____Scan over blanks before the call to nextLine,_____________________
    
    _____Or trim leading blanks off of ending before testing it.___________
    

    Over 1/4 got this. Almost 1/6 seemed to think that trimming blanks was not sufficient, and that we also needed to add additional code to skip comments.

  6. Background The code discussed in the previous problem was the body of a new static public method called lineEnd. The lineEnd(sc,msg) method contained only one non-local reference, the call to Errors.warn().

    a) Given the above, what restrictions are there on the placement of the lineEnd method? That is, are there any classes where it cannot be put? (0.6 points)

    _____We can put it in any visible class________________________________
    
    _______________________________________________________________________
    

    Over 1/4 got this, and just under 1/4 lost a bit of credit for suggesting restrictions that that hardly matter.

    Among wrong answers, over 1/12 suggested that we couldn't reasonably put it in Errors and a few thought putting it in class Road or Main were interdicted. 1/12 left this blank.

    b) Is there a logical reason to put it in some specific class? If so, give the reason. (0.6 points)

    _____It is a scanner helper method, so put it in ScanSupport.__________
    
    _______________________________________________________________________
    
    

    Over 1/6 got this. Among those earning partial credit, almost 1/4 thought the best place for this code was in Main and a few ruled out all the application dependent classes without suggesting a final destination for the code.

    Over 1/12 left this blank.

  7. Background Looking through the methods of class Scanner, there are many pairs of methods with the same name and similar descriptions where

    A question: Which of these methods is likely to be the basic method, and which do you think is a wrapper or convenience method and why? (0.6 points)

    _____Pattern are basic,________________________________________________
    
    _____String representations of patterns are convenience wrappers.______
    

    Over 7/12 got this.

    Among those earning no credit, most got it backward. A common but utterly false argument was that strings are simple values, while objects are complicated things, so the string must be basic and complex patterns would have to be translated to strings before use.

    In fact, strings are not simple values, they are complicated objects, and strings that represent patterns must themselves be translated into pattern objects before use. One astute student said that the fact that the string was named "pattern" was a strong hint about this.