Problem #1
Expression Value
-----------------------------------------------
5 * 6 - (4 + 3) * 2 - 2 * 3 10
208 / 20 / 4 + 12 / 10.0 + 0.4 * 2 4.0
8 - 2 + "8 - 2" + (8 - 2) + 8 "68 - 268"
4 * 5 % 6 + 297 % 10 + 4 % 8 13
13 / 2 * 3.0 + 5.5 * 3 / 2 26.25
2 points each, 1 point for correct value of wrong type (missing parens, no .0)
-------------------------------------------------------------------------------
Problem #2:
solution:
he says can we yes
Barry says maybe we can
we says he we can
Sarah says no we can't
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(14, 14); 14 13
ifElseMystery(4, 5); 7 5
ifElseMystery(10, 5); 12 10
ifElseMystery(2, 8); 3 8
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(0); 1 1
mystery(7); 2 12
mystery(32); 3 123
mystery(256); 4 1234
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 z % 2 == 0
+---------------------+---------------------+---------------------+
Point A | sometimes | always | always |
+---------------------+---------------------+---------------------+
Point B | always | sometimes | sometimes |
+---------------------+---------------------+---------------------+
Point C | sometimes | never | always |
+---------------------+---------------------+---------------------+
Point D | sometimes | never | never |
+---------------------+---------------------+---------------------+
Point E | never | 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();
}
1 header
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
2 looping m times (either loop goes m times or fencepost + loop goes m-1 times)
1 attempt
1 correct
2 printing the multiples of n inside a loop
1 reasonable attempt (prints something that involves n and i)
1 correct (correctly computing and printing the multiples with commas)
2 solved the fencepost
1 attempt (printed one of the numbers outside the loop, this would have
affected their loop bounds scoring)
1 correct (requires printing commas in a loop and loop bounds are within
obob)
Problem #7
Solutions:
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;
}
public static int digitRange(int n) {
int min = 9;
int max = 0;
int power = 1;
while (power * 10 < n) {
power = power * 10;
}
while (power > 0) {
int digit = n / power;
n = n - digit * power;
power = power / 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)
3 max/min computation (assuming digits are computed correctly)
1 some attempt at max/min computation (adjacent difference counts)
1 correct initialization of max and min:
max initialized to a digit or something <= 0
min initialized to a digit or something >= 9
1 correctly updates max and min 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)
Solutions:
public static boolean hasAlternatingParity(int[] list) {
for (int i = 0; i < list.length - 1; i++) {
if (list[i] % 2 == list[i + 1] % 2) {
return false;
}
}
return true;
}
public static boolean hasAlternatingParity(int[] list) {
if (list.length > 0) {
int previous = list[0] % 2;
for (int i = 1; i < list.length; i++) {
int next = list[i] % 2;
if (previous == next) {
return false;
}
previous = next;
}
}
return true;
}
2 header (-1 per error)
2 loop structure and initialization to be able to compare adjacent elements (or
compare current against previous)
1 attempt (some for loop over array size)
1 correct (either loop n-1 times or store previous and correctly handle
empty array)
4 adjacent parity test
2 attempt (probably atomic)
2 correct (-1 if close--ask question leader)
2 true/false/return structure: correctly returns true/false based on a test in
a loop (requires some kind of test in a loop, but assume test works). This
is generally atomic but can be worth 1 point for solutions that are close.
For example, if their only error is that they forgot "return true" after the
loop, they get 1 point. Or their only error might be to fail to return true
for an array of length less than 2, in which case they get 1 point. If they
switch true/false or if they prematurely return true, they lose both points.
Ask the question leader if you see something that might be close.
Problem #9
Solutions:
public static void distributeTokens(int[] tokens, int player) {
int n = tokens[player];
tokens[player] = 0;
for (int i = 1; i <= n; i++) {
tokens[(player + i) % tokens.length]++;
}
}
public static void distributeTokens2(int[] tokens, int player) {
int num = tokens[player];
tokens[player] = 0;
while (num > 0) {
player++;
if (player == tokens.length) {
player = 0;
}
tokens[player]++;
num--;
}
}
Can get up to 3 points for any of the following good things:
1 correct header
1 saving tokens[player] in a local variable
1 setting tokens[player] to 0 before a loop
1 subtracting 1 from a token count in a loop
1 loop using token count
1 tokens[i]++
Pass into round 2 if it has some hope of working:
* they save the value of tokens[player] in a local variable
* they use the saved value to control a loop
* they have some attempt to wrap back to the beginning of the array
When in doubt, pass on to round 2.
round 2 deductions:
-2 not resetting tokens[player] to 0 before loop
-1 forgetting to do i++ in their loop
-1 obob on test for array length
-1 skipping current player
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011