Problem #1 Expression Value ----------------------------------------------- 3 * 4 + 5 * 6 42 23 % 5 - 17 % (16 % 10) -2 "1" + 2 + 3 * 4 + (5 + 6) "121211" 1.5 * 2 + 20 / 3 / 4.0 + 6 / 4 5.5 345 / 10 / 3 + 10 / (5 / 2.0) 15.0 2 points each, 1 point for correct value of wrong type (missing parens, no .0) ------------------------------------------------------------------------------- Problem #2: solution: foo wants buzz to sue. bill wants sue to buzz. bill wants sue to hope. hope wants bill to sue. 1 point for each of the 12 values -1 if any error in surrounding text (dittos not allowed) -4 if two columns transposed -8 if you need to transpose twice -0 if quoted ------------------------------------------------------------------------------- Problem #3: Method Call Output Produced --------------------------------------- ifElseMystery(1, 8); 3 8 ifElseMystery(3, 5); 5 0 ifElseMystery(4, 5); 5 6 ifElseMystery(8, 6); 8 2 each is 3 points, -1 for a single number wrong or for a single transpose -2 overall if columns transposed -2 if formatting error (e.g., commas between numbers) -1 if individual pair is reversed -0 if output is quoted -0 if underscore for space ------------------------------------------------------------------------------- Problem #4: Method Call Value Returned -------------------------------------- mystery(3) 2 mystery(4) 3 mystery(5) 5 mystery(6) 8 3 points each Problem #5: y > x z < 0 z > 0 +---------------------+---------------------+---------------------+ Point A | sometimes | never | never | +---------------------+---------------------+---------------------+ Point B | never | never | sometimes | +---------------------+---------------------+---------------------+ Point C | sometimes | never | always | +---------------------+---------------------+---------------------+ Point D | always | never | sometimes | +---------------------+---------------------+---------------------+ Point E | always | sometimes | sometimes | +---------------------+---------------------+---------------------+ 1 point per blank Problem #6 Solution: public static void printMultiples(int n, int times) { System.out.print("The first " + times + " multiples of " + n + " are " + + n); for (int i = 2; i <= times; i++) { System.out.print(", " + i * n); } System.out.println(); } 2 header (-1 per error) 2 printing leading text ("The first..."); 1 reasonable attempt (includes both parameters and some of the text) 1 correct (all correct, not inside or after the loop) 1 exactly one println at the end 3 looping m times (either loop goes m times or fencepost + loop goes m-1 times) 1 any kind of loop 1 within obob of correct bounds 1 correct bounds 3 printing the multiples of n inside a loop 1 reasonable attempt (prints something that involves n and i) 2 correct (correctly computing and printing the multiples with commas) 4 solved the fencepost 1 attempt (printed one of the numbers outside the loop, this would have affected their loop bounds scoring) 3 correct (requires printing commas in a loop and loop bounds are within obob; mostly atomic, -1 if minor error, ask question leader about -1) Problem #7 Solution: public static int digitRange(int n) { int min = n % 10; int max = n % 10; while (n > 0) { int digit = n % 10; n = n / 10; if (digit < min) { min = digit; } if (digit > max) { max = digit; } } return max - min; } 1 correct header 4 loop to get digits 1 attempt to use % 10 to get a digit 1 correctly uses % 10 to get a digit 1 correctly updates n to n / 10 1 correct test (n != 0 or n > 0) 8 max/min computation 1 some attempt at max/min computation 3 correct initialization of max and min 1 max initialized to a digit or something <= 0 1 min initialized to a digit or something >= 9 1 all correct: either a digit or works in all cases including 0 2 correctly updates min (min digit) in the loop 2 correctly updates max (max digit) in the loop 2 returning their result 1 returns something 1 correctly returns (max digit) - (min digit), but no double jeopardy if they had an error in computing those OR returns a max of some digit delta (e.g., difference of adjacent digits) Problem #8 Solutions: public static String undouble(String s) { String result = ""; if (s.length() > 0) { char last = s.charAt(0); result += last; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) != last) { result += s.charAt(i); } last = s.charAt(i); } } return result; } public static String undouble(String s) { String result = ""; if (s.length() > 0) { result = s.substring(0, 1); for (int i = 1; i < s.length(); i++) { if (s.charAt(i) != s.charAt(i - 1)) { result += s.charAt(i); } } } return result; } Can get up to 3 points for any of the following good things: 1 correct header 1 for loop involving s.length 1 returning a String 1 s.charAt(i) or s.substring(i, i + 1) 1 str += charAt or substring 1 comparing charAt(i) with charAt(i + 1) (or substring) If both of the last two (concat & adjacent comparison), then give to Stuart to grade for score of 4-9. -1 if fails on empty string or case like ccaatt -2 if their code doesn't handle 1st/last char properly -0 String result = s.charAt(0); -0 for String new = ... (reserved word)
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011