CS305j Fall 2006 Midterm 1 Key, Suggested Solutions, and Grading Criteria Abbreviations: NAP - no answer provided OBOE - off by one error GCE - misunderstood question. Answer is way off base. 1. 2 points per question 18 6 "7225" 10.5 3 Comments. The most difficult one was the letter C. Going from left to right the first + sign is integer addition. The next is String concatenation. Now that one of the operands is String the other + signs are String concatenation as well. 2 + 5 + "2" + 2 + 5 | 7 + "2" + 2 + 5 | "72" + 2 + 5 | "722" + 5 | "7225" -1 for forgetting the quotes on "7225" to show the result was a String and not an int. D. Also had some complexity. 2.5 + 5 / 2 + (3 / 0.5) | 2.5 + 5 / 2 + 6.0 | 2.5 + 2 + 6.0 | 4.5 + 6.0 | 10.5 2. 3 points per line 4 2 5 6 3 8 4 2 4 6 3 5 2 3 A number of people missed the println in the main method. 3. 3 points per answer 1 1 2 4 8 A number of people got the first 2 calls wrong. If the parameter is -1 or 0 the for loop simply does not execute. (In other words it executes zero times.) If the body of the for loop never executes the value inside the variable total, which was initialized to 1, is never changed, and the method returns 1. 4. Basic breakdown of points Circles -> 9 points Lines -> 6 points Partial credit possible The window will contain 9 complete circles, 3 rows of 3 each. There will be 2 diagonal lines, one from the upper left corner to the lower right and one from the upper right corner to the lower left. 5.Basic breakdown of points: Correct number of .s -> 5 points Correct digits -> 5 points Correct Structure -> 5 points Correct answer: .....11111 ....2222 ...333 ..44 .5 Here were some common errors. All output on one line i's instead of the value of i Wrong number of .s or i's such as .1 ..22 ...333 ....4444 .....55555 6. Basic breakdown of points return type of int -> 2 points rest of header -> 1 point parameters -> 4 points calculation -> 5 points return -> 3 points Some common problems. Asking user for input or printing out result. The specification said nothing about printing out result. Doing so is incorrect. The information is given meaning it is passed via parameters to the method. The method is not to ask the user for input. Hard coding a solution to the example. Remember, the whole point of programs is to generalize solutions so that we can solve many problems, not just one particular one. Never hard code to the example. Suggested solution: public static int getScore(int red, int blue, int yellow){ return red * 10 + blue * 5 + yellow; } 7. Basic breakdown of points method header correct -> 3 points outer loop for number of lines -> 3 points inner loop for one line of output -> 3 points print out char correctly -> 2 points track correct char to print out some way -> 3 points println statement so output breaks across several lines -> 1 point Certainly the hardest problem on the test. Tracking the right char to print out was the most complicated part. Suggested solution: public static void breakString(String s, int numChars){ int numLines = s.length() / numChars; int charIndex = 0; for(int line = 0; line < numLines; line++){ for(int i = 0; i < numChars; i++){ System.out.print(s.charAt(charIndex)); charIndex++; } System.out.println(); } }