Problem #1 Expression Value ------------------------------------------------- 16 / 3 + 3.2 * 2 11.4 8 / 7 + "5 / 4" + 8 / 3 "15 / 42" 88 % 10 % 3 * 16 / 10 3 29 / 3 / 2 / 4.0 + 3.6 * 2 8.2 1.4 + (3 + 2 * 6) / (8 - 14 / 3) * 2.2 8.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 -0 if quoted ------------------------------------------------------------------------------- Problem #3: Method Call Output Produced --------------------------------------- ifElseMystery(4, 15); 6 15 ifElseMystery(7, 17); 9 17 ifElseMystery(12, 5); 12 1 ifElseMystery(16, 8); 16 4 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 Output Produced --------------------------------------- mystery(4); 2 2 mystery(5); 1 5 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 void printTwoDigit(Random r, int n) { boolean seen42 = false; for (int i = 1; i <= n; i++) { int number = r.nextInt(90) + 10; System.out.println("next = " + number); if (number == 42) { seen42 = true; } } if (seen42) { System.out.println("We saw a 42"); } else { System.out.println("no 42"); } } 2 header (-1 per error) 2 looping n times 1 within obob of correct bounds 1 correct bounds 4 choosing random 2-digit number 1 reasonable attempt to generate a 2-digit number somewhere 1 correct expression for generating 2-digit number somewhere 2 computes a new random value each time through the loop 2 println inside the loop to print 2-digit number (no double jeopardy with choosing the numbers to print) 1 attempt 1 correct 5 Did we see a 42? 1 they have both printlns ("We saw a 42" and "no 42") 2 some kind of flag or counter variable that is updated when 42 is seen (exception: -1 for small mistakes, such as checking for 24 instead of 42) 1 correct initialization of that counter/flag 1 counter/flag variable used correctly with an if/else or if/if after the loop to choose between the two printlns Problem #7 Solutions: public static boolean hasOddEvenDigit(int n) { boolean hasEven = false; boolean hasOdd = false; while (n > 0) { int digit = n % 10; n = n / 10; if (digit % 2 == 0) { hasEven = true; } else { hasOdd = true; } } return hasEven && hasOdd; } public static boolean hasOddEvenDigit2(int n) { int evens = 0; int odds = 0; while (n > 0) { if (n % 2 == 0) { evens++; } else { odds++; } n = n / 10; } return evens > 0 && odds > 0; } 1 correct header 1 uses % 10 to get the last digit or % 2 to find out if last digit is even 4 loop to examine digits (don't worry about potential early return for this) 2 correctly updates n to n / 10 once each time through loop 2 correct test to process all digits: n != 0 or n > 0 (-1 if obob like n / 10 != 0 or bad placement of n = n / 10) 6 recognizing odd/even digits 3 state variable to keep track of whether an even digit has been seen 1 declared and initialized properly 2 updated properly inside the loop 3 state variable to keep track of whether an odd digit has been seen 1 declared and initialized properly 2 updated properly inside the loop note: if the variables are never declared/initialized or declared only inside the if, then they lose both of the declare/initialize points AND a syntax point, but they can get the update points 3 returning their result 1 reasonable attempt to return an appropriate value (can include checking adjacent digits for an odd/even pair or returning after a loop that is testing for odd/even digits) 2 correctly uses odd/even state variables to return whether both were seen (lose these points if they return early, -1 if minor error--ask question leader if it's a minor error) Problem #8 Solutions: public static void printStripped(String s) { boolean inComment = false; for (int i = 0; i < s.length(); i++) { char next = s.charAt(i); if (next == '<') { inComment = true; } else if (inComment && next == '>') { inComment = false; } else if (!inComment) { System.out.print(next); } } System.out.println(); } public static void printStripped(String s) { int start = s.indexOf('<'); while (start != -1) { int stop = s.indexOf('>', start + 1); s = s.substring(0, start) + s.substring(stop + 1); start = s.indexOf('<'); } System.out.println(s); } 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 s.indexOf("<") or s.indexOf(">") 1 printing an individual character 1 printing a substring that involved s.indexOf("<") or s.indexOf(">") 1 some kind of flag to keep track of whether you are in a comment To make it to round 2, they have to have either some kind of flag that keeps track of whether or not they are in a comment (comparing against "<" and ">") or combinations of s.subtring and s.indexOf("<") and s.indexOf(">")
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011