




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





Spring 2001
Exam information
369 students took the exam. Scores ranged from 1 to 20, with a median of 11 and an average of 11.1. There were 40 scores between 15.5 and 20, 180 between 10.5 and 15, 132 between 5.5 and 10, and 17 between 2 and 5. (Were you to receive a grade of 16 on all your midterm exams, 48 on the final exam, plus good grades on homework and lab, you would receive an A–; similarly, a test grade of 11 may be projected to a B–.)
There were four versions of the exam, A, B, C, and D. (The version indicator appears at the bottom of the first page.) Versions A and C were identical except for the (reversed) order of the problems. Versions B and D were also identical except for the order of the problems.
If you think we made a mistake in grading your exam, describe the mistake in writ- ing and hand the description with the exam to your lab t.a. or to Mike Clancy. We will regrade the entire exam.
Solutions and grading standards for versions A and B
Problem 0 (1 point)
You lost 1 point on this problem if you did any of the following:
The reason for this apparent harshness is that exams can get misplaced or come unstapled, and we would like to make sure that every page is identifiable. We also need to know where you will expect to get your exam returned. Finally, we occasion- ally need to know where students were sitting in the class room while the exam was being administered.
Problem 1 (2 points)
This was problem 4 on versions C and D. Version A asked for a statement that would initialize the joeAcct variable as shown in the diagram. Here are solutions:
Account joeAcct = new Account (100, new Account (500)); Account joeAcct = new Account (100, new Account (500, null));
Version B asked for essentially the same thing, except initializing maryAcct with dif- ferent balances:
Account maryAcct = new Account (300, new Account (700)); Account maryAcct = new Account (300, new Account (700, null)); (^1) ⁄ 2 point was deducted for not declaring the variable, that is, omitting the first Account on the line, or for forgetting either new. 1 point was deducted for a correct two-statement solution; a two-statement solution with some other small error received 1 ⁄ 2 point out of 2. 1 point was also deducted or a solution with an incorrect attempt to initialize a new overdraft account, for example
Spring 2001
Account maryAcct = new Account (300, Account parentAcct=new Account(700));
No credit was given for any solutions that did not explicitly initialize the overdraft account. For example,
Account maryAcct = new Account (300, myParent);
earned 0 out of 2.
Problem 2 (8 points)
This was problem 3 on versions C and D.
Part a, worth 2 points, involved identifying errors in a main program that tested the IslamicDate class. Answers are shown below.
public static void main (String [ ] args) { IslamicDate d1 = IslamicDate (1, 1); error (new omitted; corrected by inserting new) IslamicDate d2 = d1.makeTomorrow ( ); error (a call to a void method is used where an IslamicDate expression is required) IslamicDate d3 = null; OK System.out.println ("" + d1); OK System.out.println (d3.tomorrow ( )); error (d3 is null) }
In version B, the fourth statement was moved to precede the assignment to d2; thus the answers were error, OK, error, OK, error. 1 ⁄ 2 point was deducted for each error, up to a maximum of 2.
In part b, worth 6 points, you were to write a method precedes (version A) or isLater- Than (version B) that could be used outside the IslamicDate class to compare two dates in the same year. (We explain only the precedes method here. isLaterThan is the same, except with appropriate comparisons reversed.) There are a variety of solution approaches, most resembling the daySpan computations of homework 2; examples appear below.
An iterative solution:
public static boolean precedes (IslamicDate d1, IslamicDate d2) { if (d1.equals (d2)) { return false; } IslamicDate d3; for (d3=d1.tomorrow ( ); !d3.equals (d2) && !d3.equals (new IslamicDate (12,29)); d3.makeTomorrow ( )) { } return d3.equals (d2); }
A while loop or a rearranged for loop would also have been appropriate.
Spring 2001
Since the precedes method is to be called from DateTester.main without any DateTester object having been initialized, it must be declared as static. 1 point was deducted for omitting the static modifier, or for any other error in the method header. (Omitting the IslamicDate declarations from the parameter list, i.e. saying
public static boolean precedes (d1, d2)
was a somewhat less common way to lose this point.) We did not deduct any points for declaring the method as private rather than public.
There were numerous opportunities for logic errors. An off-by-one error generally lost 1 point. Off-by-one errors included both errors in data comparisons and errors in String processing for those who used the toString approach. It also included solutions that returned true if the dates were equal. Most other minor logic errors, e.g. using an incorrect comparison operator or nesting ifs incorrectly, also lost 1 point.
A more serious logic error was to somehow compute the number of days between the two dates, without checking whether one has wrapped around past the end of the year when computing this difference. This often involved a comparison with the total number of days in a year, but with no comparison to the first or last day of the year. This earned a 2-point deduction. Even more serious was to handle only consecutive dates, for example by calling d1.tomorrow ( ) and comparing the resulting date to d without a loop or recursion. This error lost 5 points.
Other errors, all worth a 1-point deduction, included the following:
Problem 3 (5 points)
This problem involved analyzing code similar to that from lab assignment 3. It was problem 2 on versions C and D. Version A’s method was
public boolean contains1MoreThan (String s) { if (myString.length( )==0) { return false; } else if (s.length( )==0) { return true; } else { StringToCheck5 remainder = new StringToCheck5 (myString.substring(1)); if (myString.charAt(0) == s.charAt(0)) { return remainder.contains1MoreThan (s.substring(1)); } else { return remainder.contains1MoreThan (s); } } }
Spring 2001
Version B’s code was the same, except that the order of the first two comparisons was switched: if (s.length( )==0) { return true; } else if (myString.length( )==0) { return false; } else { ...
Part a, worth 2 points, was to determine if the method would crash. You earned 1 point for the answer and the other for the explanation. The answer we expected was “no”, since the length comparisons guarded the subsequent uses of charAt and sub- string. Since grading the exam, however, we noticed that the method precondition did not rule out a value of null for myString. We will award 1 or 2 points for the answer “yes”, provided that your explanation mentions the possibility of myString containing null. (Whether you get 1 or 2 points depends on the rest of your answer for that part.)
A common error on part a was to claim that a crash would result from myString or s containing exactly one character. Note, however, that charAt(0) is the String equiva- lent of Scheme’s car function; similarly, the String equivalent of cdr is substring(1). Applying substring(1) to a one-character string gives the empty string and produces no error. (StringToCheck2.contains1MoreThan from lab assignment 3 involves the same operations, and doesn’t crash.) The erroneous claim may have resulted from confusing the empty string with a null String reference.
Questions for part b differed in the two versions. On version A, you were to describe all pairs of Strings myString and s for which contains1MoreThan should return true but doesn’t. On version B, we wanted just the opposite: a description of all pairs of Strings myString and s for which contains1MoreThan should return false but doesn’t. Here’s the analysis a correct answer might have involved.
Spring 2001
Problem 4 (4 points)
This problem was based on lab assignment 4. Version A was identical to version B except that the parts were reversed. It was problem 1 on versions C and D.
Here is an Account constructor that throws IllegalArgumentException if its argument is negative. (The assumption is that it would replace the 1-argument constructor cur- rently provided in the class.)
public Account (int balance) { if (balance < 0) { throw new IllegalArgumentException ("Balance < 0:" + balance); } myBalance = balance; myParent = null; }
IllegalArgumentException is an “unchecked” exception, so you don’t need throws Ille- galArgumentException in the header; it doesn’t hurt to include it, though. The assign- ment statements may come either before or after the balance test. You don’t need the assignment to myParent since it would be set to null by default.
1 point was deducted from the constructor solution for each of the following errors; both were missing some important part of the exception throw.
Bad logic (usually throwing the exception for a positive balance) also received a 1- point deduction. Solutions that had all the necessary parts received a 1 ⁄ 2 -point deduc- tion for incorrectly specified parts, for example:
The other part of this problem involved testing a version of the 1-argument construc- tor written by someone else: “initialize an Account with a negative balance and then print a suitable message about what happened”. A full-credit solution printed some message regardless of whether or not the constructor threw the exception. Here’s a solution.
public static void main (String [ ] args) { try Account test = new Account (-3); System.out.println (“Account initialized with negative balance!”); } catch (Exception e) { System.out.println ("Exception correctly thrown "
Spring 2001
A frequent error was the failure to include a call to println immediately after the con- structor call, to print an error message if the constructor did not throw an exception. This lost 1 point. (This error occurred so often that some graders resorted to short- hand: “DPSM” means “Didn’t Print Suitable Message”.) Other 1-point errors were access of Account private variables—the main program may be in a “Tester” class rather than in the Account class—and method calls with incorrect syntax, e.g. using acct.balance instead of acct.balance ( ).