Exam 2: 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 II

Mean   = 4.98
Median = 4.6      X
                  X X
        X         X X X   X X X           X
        X X X X X X X X X X X X X     X   X
        X X X X X X X X X X 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

Exam I + II

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

Machine Prolbems 1 – 4

                                      X
Mean   = 15.16                X       X X
Median = 15.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_X_____
   0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20

Homework 1 – 9

Mean   = 19.65                        X
Median = 19.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____
   0 . 2 . 4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26

Total Scores

                                                 X       X
                                       X X     X X       X
                                       X X     X X       X
Mean   = 44.24                       X X X     X X       X
Median = 44.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_________
   0 . 4 . 8. 12. 16. 20. 24. 28. 32. 36. 40. 44. 48. 52. 56. 60. 64. 68
            F     |     D     |     C     |     B     |     A     |

These letter grades should be considered preliminary. Homework scores have not been adjusted to count only the to 10 of 12 assignments, and there are still 2 machine problems and a final.

Exam II Solutions

  1. Consider the following Java code (this code has been tested, it actually works):
    ClassA p = (int i) -> 2 * i;                                     // a
    ClassB q = (ClassA j) -> ( (int k) -> j.x( j.x( k ) ) );         // b
    ClassA r = q.y( p );                                             // c
    int    s = r.x( 1 );                                             // d
    

    a) You can infer a complete definition for ClassA from the above. Give it. (1.0 points)

    _____interface ClassA {________________________________________________
    
    _________int x( int i );_______________________________________________
    
    _____}_________________________________________________________________
    

    The parameter name i is arbitrary, but the rest is pinned down by the code given in the problem statement.

    1/5 did well, 1/4 earned no answer, although almost all wrote some kind of nonsense. 1/5 earned a small penalty for using the keyword class instead of interface. More serious penalties were assigned for giving a method body of some kind, or for failing to include a method named x.

    b) What is the type of the subexpression j.x( j.x( k ) ) (0.5 points)

    _____int_______________________________________________________________
    

    3/4 earned full credit; there was no partial credit.

    c) The subexpression ( (int k) -> j.x( j.x( k ) ) ) creates a new object. To what class does that object belong? Be as specific as you can. (1.0 points)

    _____an anonymous implementation of ClassA_____________________________
    

    1/10 earned full credit, while half earned partial credit. The most common error was to just state ClassA (the name of the interface it implements) without specifying that a class implements that interface.

    d) You can infer a complete definition for ClassB from the above. Give it. (1.0 points)

    _____interface ClassB {________________________________________________
    
    _________ClassA x( ClassA j );_________________________________________
    
    _____}_________________________________________________________________
    

    The parameter name j is arbitrary, but the rest is pinned down by the code given in the problem statement.

    Under 1/5 got this. Problems were very similar to part a), but in addition, 1/10 used int instead of ClassA for the return value. 1/5 earned no credit by giving nonsense answers, while 1/15 left this blank.

    e) There are a number of unnecessary variables in the above code. Give code equivalent to line d that eliminates the use of variables p and r and therefore allows deletion of lines a and c. (1.0 points)

    ____int    s = q.y( (int i) -> 2 * i ).x( 1 );_________________________
    

    This was intended to be fairly easy clerical work, but only 1/4 earned full credit. 1/12 left it blank, and almost 1/3 gave nonsense answers. Fully 1/4 forgot about the trailing .x(1).

    f) What is the value of s? (1.0 points)

    _____4_________________________________________________________________
    

    Over 1/2 earned full credit, and 1/10 gaven nonsense answers of 1, 2, 6 and 16. Half credit was given to "off by one multiplication by 2" answers; 1/15 gave 2, and 1/5 gave 8.

    Note, you can put all of the above together to make a Java program to play with in order to explore this exam question. Here it is:

    public class T {
        interface ClassA {
            int x(int q);
        }
        interface ClassB {
            ClassA y(ClassA r);
        }
        public static void main( String[] args ) {
            ClassA p = (int i) -> 2 * i;
            ClassB q = (ClassA j) -> ( (int k) -> j.x( j.x( k ) ) );
            ClassA r = q.y( p );
            int    s = r.x( 1 );
        //  int    s = q.y( (int i) -> 2 * i ).x( 1 );
            System.out.println( "Value is: " + s );
        }
    }
    

  2. Background: Consider this code from a solution to MP4, class Wire.
    public void inputChange( float now, int value ) {                // a
        Simulation.schedule(                                         // b
            now + delay, (float t) -> this.outputChange( t, value )  // c
        );                                                           // d
    }                                                                // e
    private void outputChange( float now, int value ) {              // f
        final int comp = 1 - value;                                  // g
        System.out.println(                                          // h
            "time "+ now +" "+ comp +"->"+ value +" "+ this          // i
        );                                                           // j
        destination.inputChange( now, value );                       // k
    }                                                                // l
    

    a) Suppose there was no requirement for printing any output changes of any wires. How would you change line c so that lines f to l can be deleted. (1.0 points)

    _____now + delay, (float t) -> destination.inputChange( now, value )___
    
    _____(in English:  make it call destination.inputChange directly)______
    

    7/12 got this right, while 1/6 earned no credit. There were no common categories of wrong answers.

    b) Suppose we had used boolean instead of int as the type for values in our simulation. How would you rewrite line g? (0.5 points)

    ____final boolean comp = !value;_______________________________________
    

    Almost 1/2 got this, while 1/6 earned no credit. 1/6 had real difficulty negating a boolean value, resorting to complex such complex things as if-then-else statements, bizarre extra code such as !(false||value), or 1^value (in Java, true^value would have worked, but nobody gave that answer). 1/10 were penalized for confused declarations or type errors.

  3. Background: Here is another version of class Wire, where value is now a field of the wire object:
    public void inputChange( float now, int v ) {                    // a
        value = v;                                                   // b
        Simulation.schedule(                                         // c
            now+delay, (float t)-> this.outputChange( t )            // d
        );                                                           // e
    }                                                                // f
    private void outputChange( float now ) {                         // g
        final int comp = 1 - value;                                  // h
        System.out.println(                                          // i
            "time "+ now +" "+ comp +"->"+ value +" "+ this          // j
        );                                                           // k
        destination.inputChange( now, value );                       // l
    }                                                                // m
    

    a) If the interval between two successive changes to the input of this wire is longer than the wire's delay, how would the behavior change from the original version of Wire? (1.5 points)

    _____There would be no change in behavior._____________________________
    
    _______________________________________________________________________
    
    _______________________________________________________________________
    

    Over 1/3 earned full credit. There was very little partial credit, and only one person left it blank. Among wrong answers, many focused on the times at which events would occur (nothing in the alternative code changes the event times), and a surprising number used misleading metaphoric language like "the rate of change".

    b) If the interval between two successive changes to the input of this wire is shorter than the wire's delay, how would the behavior change from the original version of Wire? (1.5 points)

    _____The output value would not change, but there would be_____________
    
    _____two output change events reporting changes to that value__________
    
    _______________________________________________________________________
    

    1/5 got this. 1/3 earned no credit, mostly with incomprehensible answers, for example, focusing on event times or rates of change. Partial credit was given for assertions that there was some vague change, and that the change involved wrong output values.