Exam 2: Midterm

Solutions and Commentary

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

Grade Distributions

Midterm II

              X
              X
Mean   = 3.68 X X 
Median = 2.9  X X 
              X X 
        X X X X X 
        X X X X X X   X X       X   X
        X X X X X X   X X X X   X   X
        X X X X X X 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

Midterms I and II

                X     X
                X   X X     X
Mean   = 9.78   X X 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_X_X_X_X_X_X_X_X_X_X_X________
     0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20

Machine Problems 1-3

                                X
                                X
                                X
                                X
Mean   = 10.61                  X
Median = 11.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____
     0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16

Homeworks 1-9

                                              X
                                              X
Mean   = 18.23                    X     X     X
Median = 18.9                     X     X     X     X
                                  X     X   X X     X
                                  X     X X X X     X
                                  X     X X X X     X
                                  X     X X X X   X X X
                    X   X         X X X X X 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

Total Scores

                                          X
                                          X
                                          X
Mean   = 38.62                            X
Median = 37.7                             X
                                          X X
                                  X       X X
                                X X   X   X X X X     X X
                                X X X X   X X X X X   X X
                            X   X X X X X X X X X X 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 . 4 . 8 . 12. 16. 20. 24. 28. 32. 36. 40. 44. 48. 52. 56. 60
Rough Grade Scale          |    D   |    C   |    B   |    A   |

Exam Solutions and Commentary

  1. Background: The following code is legal Java:
    Something S = ()->aMethod( x );
    

    a) The value of the expression ()->aMethod( x ) above is a member of some class. To the extent possible, identify that class. (0.5 points)

    ____an anonymous subclass of Something_________________________________
    

    1/10 earned full credit, 1/2 said just Something for good partial credit.

    b) Class Something must contain a method. You cannot infer the name of this method, but you can infer its argument list. Give that list. (0.5 points)

    ____() -- that is, no arguments________________________________________
    

    1/3 got this, 1/3 earned no credit, giving (x) -- they were distracted by the argument to the method call inside the lambda expression. Other wrong answers were difficult to categorize, but they generally involved confusing attributes of the class Something with one or another aspect of the given lambda expression.

  2. In the solution to MP4 posted on line, there was one bit of awkward code that followed this pattern:
    SomeClass x = null;
    if (y) x = someValue;
    final SomeClass finalX = x;
    someMethod( ()->someOperation(finalX) );
    

    a) Why did we have to introduce the variable finalX? (1 point)

    ____Variables used in a lambda expression need to be___________________
    
    ____final or effectively final_________________________________________
    

    1/3 got this. 1/10 said merely final for most of the credit. There were also some vague answers. The majority of those who earned no credit tried to explain what final means instead of commenting on its application in this context.

    b) Give a one line declaration and initialization of x that eliminates the need for finalX by using a Java A Java conditional expression of the form (a?b:c). (1 point)

    ____final SomeClass x = (y ? someValue : null );_______________________
    

    1/8 got this. 1/3 earned no credit. The remainder earned partial credit. By far, the most common error involved attempting to put the declarations of x inside the conditional expression. Even more serious, 1/6 could not figure out that the conditional depends on y.

  3. Here is code for a key method of class StopLight in the road network simulator:
    private void lightChanges( float t ) {
    	direction = (direction + 1) % incoming.size();
    	Road r = incoming.get( direction );
    	for (int i = 1; i <= r.waiting; i++) this.pickRoad().entryEvent( t );
    	r.waiting = 0;
    
    	Simulator.schedule(
    		t + interval,
    		(float time)->StopLight.this.lightChanges( time )
    	);
    }
    

    a) The code above can be described as an iterated or periodic process because: (1 point)

    ____Each call to lightChanges schedules another________________________
    
    _______________________________________________________________________
    

    1/3 got this. 1/8 earned partial credit with vague answers. Among those who earned no credit, many were confused by the for loop and focused their answer on that iteration instead of the logical outer loop of this process that allows it to repeat itself.

    b) How does the simulation time get advanced from one iteration of this process to the next? (1 point)

    ____lightChages schedules itself interval time units in the future_____
    
    _______________________________________________________________________
    

    1/2 got this. 1/4 earned partial credit. Aside from vague answers, the most common (and weakest) answer earing partial credit was the assertion that time is passed as a parameter. A good way to earn no credit on this problem was to drift off into an explanation of the scheduling mechanism and lose track of the specific context of the question, this logical process.

    c) Why introduce the variable r above? It wasn't there in the code distributed to the class. (1 point)

    ____using r avoids repeated calls to incoming.get( direction )_________
    
    _______________________________________________________________________
    

    1/6 got this. 1/6 earned partial credit by identifying r as a road. Outright wrong answers frequently made bizarre assertions about the variable r or suggested that without this variable the program would not work.

    d) When lightChanges() is triggered, how many distinct events can it cause to be scheduled? (1 point)

    ____one to continue the process plus one for each waiting vehicle______
    

    1/4 gave good answers. 1/8 only identified the next iteration of the process, 1/5 only identified the events to launch waiting vehicles. 1/3 gave naked numbers that made just enough sense to be worth partial credit. Only a few earned no credit.

  4. Here is a bug note in the version of class Intersection from the road network simulator distributed to the class:
    public LinkedList  outgoing = new LinkedList  ();
    public LinkedList  incoming = new LinkedList  ();
    // Bug:  changes to incoming/outgoing forbidden after net is built.
    

    Linked lists have many methods for changing them, including add(listElement), clear(), push(listElement), pop(), and many more. Other methods, such as get(index), peek() and size() make no changes to the list.

    Our goal here is to discuss (but not complete) the work of creating a subclass of linked list that includes one new method, finalize()

    a) What variable(s) would the new class need add to the representation of each list object? (1 point)

    ____boolean isFinal = false; // the name is not critical_______________
    

    1/4 did well, 1/10 earned partial credit with vague ideas. A common way to earn no credit was to be distracted by the context and start saying something about roads or intersections. Another way to earn no credit was to focus on the keyword final, a keyword that is almost irrelevant to this problem.

    b) What method(s) other than finalize() would you need to declare in the new class (if this is a whole category of methods, describe the categotry in a few words.) (1 point)

    ____All methods that could change the contents of the list_____________
    

    1/4 did well here. 1/5 earned partial credit by at least naming a few methods, usually a mixture of those that needed to be redefined and those that didn't.

    c) Describe the essential logic of (each of) the method(s) you mentioned in part b. (1 point)

    ____if (isFinal) throw someException else super.thisMethod(...)________
    
    _______________________________________________________________________
    

    1/8 did well. Few earned partial credit. Many gave elaborate nonsense involving traversing lists, copying components, roads, intersections, and the keyword final. Apparently, they did not notice that this question was about the implementation of a subclass of linked list with a behavior that allows us to address the bug notice in our source code.