Problem #1 Expression Value ----------------------------------------------- 3 * (5 - 2) - 3 - 2 * 2 2 4 * 7 % 8 + 132 % 10 + 3 % 4 9 27 / 5 / 2 + 3.4 * 2 - 1.1 * 2 6.6 9 + 9 + "9 + 9" + 9 + 9 "189 + 999" 19 / 2 / 2.0 + 2.5 * 6 / 2 + 0.5 * 4 14.0 2 points each, 1 point for correct value of wrong type (missing parens, no .0) ------------------------------------------------------------------------------- Problem #2: solution: to walk the walk is good to hear the good is bad to feel the walk is song to feel the talk is bad 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 ------------------------------------------------------------------------------- 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(2); 1 2 mystery(4); 2 2 mystery(24); 4 3 mystery(28); 3 7 column 1 is 1 point, column 2 is 2 points -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 #5: x > y z == 0 x == y +---------------------+---------------------+---------------------+ Point A | sometimes | always | sometimes | +---------------------+---------------------+---------------------+ Point B | sometimes | sometimes | never | +---------------------+---------------------+---------------------+ Point C | always | never | never | +---------------------+---------------------+---------------------+ Point D | never | never | never | +---------------------+---------------------+---------------------+ Point E | never | sometimes | always | +---------------------+---------------------+---------------------+ 1 point per blank Problem #6 Solution: public static int quadrant(double x, double y) { if (x > 0 && y > 0) { return 1; } else if (x < 0 && y > 0) { return 2; } else if (x < 0 && y < 0) { return 3; } else if (x > 0 && y < 0) { return 4; } else { // at least one equals 0 return 0; } } 1 correct header 1 they return all of the values they compute 3 test attempts 1 reasonable attempt to test at least 1 of the 5 cases 1 reasonable attempt to test at least 3 of the 5 cases 1 reasonable attempt to test all 5 of the cases 5 five different cases correct 1 quadrant 1 (x > 0 && y > 0) correct test, always returns 1 in this case 1 quadrant 2 (x < 0 && y > 0) correct test, always returns 2 in this case 1 quadrant 3 (x < 0 && y < 0) correct test, always returns 3 in this case 1 quadrant 4 (x > 0 && y < 0) correct test, always returns 4 in this case 1 quadrant 0 (x == 0 || y == 0) correct test, always returns 0 in this case ** Special Cases ** -1 if return a String instead of int (can get header return type if it returns String) -1 per swap e.g. if quadrant 4 and 2 are backwards -1 for 1's instead of 0's -1 for <= or >= -1 scope issues -3 some consistent mistake in testing like || instead of && Problem #7 Solution: public static int minHailstoneValue(int n, int steps) { int min = n; for (int i = 1; i < steps; i++) { if (n % 2 == 0) { n = n / 2; } else { n = 3 * n + 1; } if (n < min) { min = n; } } return min; } 2 correct header (-1 per error) 2 for loop going (steps - 1) number of times 1 attempt 1 correct 3 correctly computes the hailstone sequence 1 reasonable attempt (one of these three: test, even update, odd update) 1 better attempt (two of these three: test, even update, odd update) 1 all correct 2 correctly computes the min 1 correct initialization 1 correct update (no double jeopardy with hailstone computation) 1 returns the min (requires loop attempt and attempt to update min in loop) Problem #8 Solution: public static boolean isSorted(double[] list) { for (int i = 0; i < list.length - 1; i++) { if (list[i] > list[i + 1]) { return false; } } return true; } 1 header 1 some kind of loop that iterates over the array (requires test against list.length and some kind of reference to list[<something>]) 2 correct loop bounds to do n-1 comparisons 1 within obob 1 correct 3 correctly compares adjacent values in the array 1 some kind of comparison of list[i] versus list[i + 1] or list[i - 1] 2 correct comparison (whether first > second, -1 if >=) 3 return true/false correctly 1 attempt 2 correct (lose these if they reverse or return early) -1 if they return strings instead of boolean Problem #9 Solutions: public static void printCompact(String s) { boolean inWord = false; boolean firstWord = true; for (int i = 0; i < s.length(); i++) { char next = s.charAt(i); if (next == ' ') { if (inWord) { inWord = false; } } else { // next != ' ' if (!inWord) { inWord = true; if (firstWord) { firstWord = false; } else { System.out.print(" "); } } System.out.print(next); } } System.out.println(); } Can get up to 3 points for any of the following good things: 1 correct header 1 loop using s.length or s.length() 1 calling s.charAt 1 remembering a previous character 1 testing a character to see if it is equal to ' ' or " " 1 calls on print inside a loop and a call on println() after Pass into round 2 if it has some hope of working: * they call s.indexOf(" ") or s.indexOf(' ') * their code notices transitions between space and nonspace characters (boolean flag or previous char or compare i/i+1) * they have an inner loop that either skips spaces or skips to a space When in doubt, pass on to round 2. Round 2 deductions: -3 extra strings -1 println instead of print -1 missing println() at the end -1 leading space bug -1 trailing space bug
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011