Problem #1 Expression Value ----------------------------------------------- 4 * (2 + 4) - 3 * 5 9 54 % 10 + 8 * 3 % 9 10 3 * 2 + 4 + "+" + 2 + 3 * 4 "10+212" 2.3 * 3 + 19 / 5 / 2 + 6.0 / 5 9.1 108 / 20 * 3 / 4 / 2.0 + 1.0 / 2 2.0 2 points each, 1 point for correct value of wrong type (missing parens, no .0) ------------------------------------------------------------------------------- Problem #2: solution: students in the beer = 12 beers in the dorm = 125 students in the dorm = 250 beers in the student = 4 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(4, 15); 15 18 ifElseMystery(7, 17); 27 10 ifElseMystery(12, 5); 15 10 ifElseMystery(16, 8); 28 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(6, 0) 0 mystery(8, 1) 8 mystery(3, 3) 17 mystery(4, 2) 10 3 points each -0 if output is quoted Problem #5: next < 0 next == 0 x > 0 +---------------------+---------------------+---------------------+ Point A | sometimes | sometimes | never | +---------------------+---------------------+---------------------+ Point B | sometimes | never | sometimes | +---------------------+---------------------+---------------------+ Point C | always | never | always | +---------------------+---------------------+---------------------+ Point D | sometimes | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point E | never | always | sometimes | +---------------------+---------------------+---------------------+ 1 point per blank Problem #6 Solutions: public static boolean hasMidpoint(int x, int y, int z) { return (2 * x == y + z || 2 * y == x + z || 2 * z == x + y); } public static boolean hasMidpoint(int x, int y, int z) { return (x - y == y - z || x - z == z - y || y - x == x - z); } public static boolean hasMidpoint(int x, int y, int z) { if (x == (y + z) / 2.0) { return true; } if (y == (x + z) / 2.0) { return true; } if (z == (x + y) / 2.0) { return true; } return false; } public static boolean hasMidpoint(int x, int y, int z) { double average = (x + y + z) / 3.0; if (x == average) { return true; } if (y == average) { return true; } if (z == average) { return true; } return false; } 2 header (-1 per error) 6 correctly computing if something is a midpoint 3 reasonable attempt: sum of two equals half of other (x - y) == (z - x) one of the three is the average of all 3 3 correct three different times (1 point for each correct test) 7 overall structure 2 some kind of test that midpoint == x returns true 2 some kind of test that midpoint == y returns true 2 some kind of test that midpoint == z returns true 1 returns false in all other cases (requires reasonable attempt at testing) notes: -1 using = vs == -1 returning String instead of boolean Problem #7 Solution: public static void flip(Random r, int n) { System.out.print("flips:"); int count = 0; while (count < n) { int result = r.nextInt(2); if (result == 0) { System.out.print(" H"); count++; } else { System.out.print(" T"); count = 0; } } System.out.println(); } 1 correct header (non-void return okay if they return something of that type) 4 printing the line of output: 1 printing "flips:" somewhere (could be println) 1 printing H and printing T somewhere (could be println) 1 they print either H or T in a loop, it's not a println, includes a space 1 all correct (print before loop, print inside loop, println after loop) 4 flipping 1 attempt: using Math.random or Random object to get a random value 1 using result of flipping to either print H or to print T (not both) 2 correct (inside a loop, equal probability, not Math.random; -1 if they construct their own Random object) 4 counter for n-in-a-row 1 declaring counter and initializing to 0 1 incremented for heads 2 reset to 0 for tails 2 loop structure 1 reasonable attempt: while loop with something that looks like a counter 2 correct loop test (requires a counter, but no double jeopardy for counter bugs; -1 if obob) Problem #8 Solutions: public static boolean increasingDigits(int n) { int prev = n % 10; n = n / 10; while (n > 0) { int digit = n % 10; if (digit >= prev) { return false; } n = n / 10; prev = digit; } return true; } public static boolean increasingDigits2(int n) { while (n > 9) { int digit1 = n % 100 / 10; int digit2 = n % 10; if (digit1 >= digit2) { return false; } n = n / 10; } return true; } Can get up to 3 points for any of the following good things: 1 correct header 1 while loop involving n > 0 or n > 9 or n > 99 1 using n % 10 1 setting n to n / 10 1 something that looks like a comparison of two digits returning false 1 returning true after the loop 1 using Math.pow(10, something) 1 some attempt to compute a power of 10 using x = 10 * x 1 while loop attempting to compute power of 10 that is greater than n Pass into round 2 if it has some hope of working: * keeping track of the previous digit (requires a second variable) * trying to pull apart the last 2 digits of a number for a comparison When in doubt, pass on to round 2.
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011