Exam 1: Midterm

Solutions and Commentary

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

Grade Distributions

Exam I

                          X
                          X X
Mean   = 6.34             X X
                          X X X     X
                          X X X X   X
                          X X X X   X     X
                X         X X X X X X     X
                X   X     X X X X X X X   X
          X     X X X X X X X X X X X X X X
          X     X X X X X X X X 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 problems 1 and 2

                                          X
                                  X       X
                                  X       X
                                  X       X
Mean   = 7.48                     X       X
                                  X       X
                        X       X X   X X X
                        X       X X   X X X
                        X       X X X X X X
                        X     X X X X X X X
                        X     X X X X X X X X
                    X   X X   X X X X X X X X
    X               X   X X   X X 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

Homeworks 1 through 5

                                                      X
                                                      X
                                                      X X
Mean   = 10.67                                    X   X X X X
                                                  X   X X X X
                                        X     X   X X X X X X
                                        X     X   X X X X X X
                        X         X   X X X   X X X X X X X X X
                  X   X X X     X X X X X X X X X X X X X X X X
____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 of the above Scores

                                                  X   X
                                            X     X   X
Mean   = 24.13                        X     X     X X X
                                X     X X   X     X X X X
                              X X     X X   X     X X X X   X
                      X       X X   X X X X X X X X X X X   X X
                X     X     X X X   X X X X X X X X X X X X X X
______X_______X_X_X___X_X___X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X_X______
   4 . 6 . 8 . 10. 12. 14. 16. 18. 20. 22. 24. 26. 28. 30. 32. 34. 36
           F     |     D     |     C     |     B     |     A     |

The above grade scale was used to assign midterm grades. Note that in the final grades, only the top 10 homework scores will be counted.

Exam I Solutions and Commentary

  1. Background: In the posted solution to MP2, if the line wire with nothing following appeared in the input file, there would be several error messages from the constructor in class Wire and the output would include a line stuffed with default values:
    wire ??? ??? 99.999
    

    The constructor was called by this code in TernaryLogic.initializeTernary():

    } else if ("wire".equals( command )) {
            wires.add( new Wire( sc ) );
    

    a) How could you change the Wire constructor to suppress creation of the useless wire illustrated above. (1 point)

    _____Make it throw an exception________________________________________
    
    _______________________________________________________________________
    

    Just under 1/2 did well, while 1/2 earned no credit; a few earned partial credit for fumbling about without being entirely wrong. This problem was based on homework 5, problem 1, for which answers were posted before the exam. Evidently, many students failed to make the connection.

    b) Briefly describe what you would add to the code given above to complement the change from part a. (1 point)

    _____Add an exception handler around the call to wires.add()___________
    
    _______________________________________________________________________
    

    About 1/4 did well here, while 2/3 earned no credit. Again, a few earned partial credit.

  2. Background: Consider this code: if((a==null)||(a.x()))b.y();

    a) If a is null, why doesn't a.x() throw an exception? (1 point)

    _____The || only evaluates its right term if the left one is false_____
    
    _______________________________________________________________________
    

    7/8 earned full credit; this was an easy problem. Among those earning little or no credit there were no obvious patterns.

    b) Rewrite this code using only if statements as control structures. (1 point)

    _____if (a==null) b.y();_______________________________________________
    
    _____else if (a.x()) b.y();____________________________________________
    
    _______________________________________________________________________
    
    _______________________________________________________________________
    

    This was harder than part a. Only about 3/4 earned full credit. The most common error was to omit the word else, so that the answer was composed of two unrelated if statements.

    Parts a and b were based on problem 1 from Homework 3, replacing && with ||. Several of the students earning no credit simply regurgitated answers to that problem here, without noticing that the change from && to || also changes the control structure.

  3. Background: Here's a simplified version of MP1:
    class Listargs {
    	public static void main(String args[]) {
    		for (String s: args) {
    			System.out.println( s );
    		}
    	}
    }
    

    A problem: Rewrite the for loop as a while loop: (2 points)

    class Listargs {
    	public static void main(String args[]) {
    
                    ___int i = 0;________________________________________
    
                    ___while (i < args.length) {_________________________
    
                    ________System.out.println( args[i] );_______________
    
                    ________i = i + 1;___________________________________
    
                    ___}_________________________________________________
            }
    }
    

    Just under 2/3 did well, mostly by giving answers equivalent to the above, while 1/4 earned no credit. Among those earning partial credit with variations on the above, forgetting to incrment the control variable and giving wrong terminating conditions on the while loop were common errors.

    Another answer this problem uses an iterator; this is closer to how the compiler actually sees the original for loop but it is also harder to remember the details:

    class Listargs {
    	public static void main(String args[]) {
    
                    ___Iterator  i = args.iterator();____________
    
                    ___while (s.hasNext()) {_____________________________
    
                    ________String s = i.next();_________________________
    
                    ________System.out.println( s );_____________________
    
                    ___}_________________________________________________
            }
    }
    

    Among the few who gave variations on this answer, few did well. Common errors included failing to distinguish between the iterator and the array, failing to initialize the iterator, and failing to advance the iterator.

  4. Background: Consider a working Java program that might contain these keywords:
    [ ] public     (x)
    [x] private    (*)
    [x] protected  (x)
    [ ] static     ( )
    [x] final      ( )
    

    a) In the square box to the left of the keyword, check the keyword(s) that you could probably delete without damaging the program. (1 point)

    Just under 1/3 did well here. Just under 1/2 thought you could freely delete the keyword public, while only a few thought that deleting static would be harmless.

    About 1/3 did not think that the keyword private could be deleted, despite the fact that homework 4, problem 1-d covered this issue. Fewer were worried about deleting protected, while more were worried about deleting final.

    b) In the round box to the right of the keyword, check the keyword(s) that could probably replace private without damaging the program. (1 point)

    This was a bit easier than part a. About 2/5 did well. About 1/2 did not think that replacing private by public was likely to wrok, while about 1/4 thought replacing private by protected was likely to cause trouble.

    A surprising 1/4 thought private could be replaced with final. and 1/8 thought private could be replaced with static.

    * Some people worried about whether replacing private with private should be checked. Replacing private with private is not making any change, so this check box was ignored in the answer.

    Note that these problems all used "probably" because there are obscure and in many cases, artificial examples where some of these substitutions or deletions might cause damage. A very few students might have lost credit for coming up with such cases.

  5. Background: Given that ScanSupport.lineEnd(m) outputs the message m if there is an error on the end of the current line, consider this code at the end of a constructor for our road network program:
    ScanSupport.lineEnd( this.toString() );
    

    a) What wasted computation does this code do when the line ends without error? (1 point)

    _____Calling this.toString() to create a string that is not used_______
    
    _______________________________________________________________________
    

    About 3/5 did well here, and 1/5 earned no credit. It was difficult finding patterns in the errors, although some students seemed distracted by issues surrounding the internal function of ScanSupport.lineEnd() in the code they had worked with instead of focusing on the information given in the exam.

    This question was closely related to issues discussed in lecture, so it is surprising that it caused problems.

    b) When you call this.toString() within a constructor, what might go wrong that would not be a problem with calls elsewhere? (1 point)

    _____Some fields of this might not yet have been initialized___________
    
    _______________________________________________________________________
    

    Around 1/3 did well here, while around 1/2 earned no credit. Wrong answers were frequently strange, for example, proposing that the code would use the wrong this.toString() method or the default method.