CSE142 Midterm This exam is divided into nine questions with the following points: # Problem Area Points Score --------------------------------------------- 1 Expressions 10 _____ 2 Parameter Mystery 12 _____ 3 If/Else Simulation 12 _____ 4 While Loop Simulation 12 _____ 5 Assertions 15 _____ 6 Programming 15 _____ 7 Programming 15 _____ 8 Programming 9 _____ 9 Bonus 1 _____ ---------------------------- Total 100 _____ This is a closed-book/closed-note exam. Space is provided for your answers. There is a "cheat sheet" at the end that you can use as scratch paper or to write answers. You can also request scratch paper from a TA. You are not allowed to access any of your own papers during the exam. The exam is not graded on style and you do not need to include comments, although you are limited to the constructs included in chapters 1 through 5 of the textbook. You are allowed to abbreviate "Always", "Never," and "Sometimes" as "A", "N", and "S" for the assertions question, but you should otherwise NOT use any abbreviations on the exam. You are NOT to use any electronic devices while taking the test, including calculators. Anyone caught using an electronic device will receive a 10 point penalty. Do not begin work on this exam until instructed to do so. Any student who starts early or who continues to work after time is called will receive a 10 point penalty. If you finish the exam early, please hand your exam to the instructor and exit quietly. 1. Expressions, 10 points. For each expression in the left-hand column, indicate its value in the right-hand column. Be sure to list a constant of appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes). Expression Value 8 * 2 - 2 * 3 ________________ 87 % 10 + 28 % 5 % 2 ________________ 1 + 2 + "3" + 4 + 5 * 6 ________________ 2 * 2.3 + 5 / 2 + 19 / 4 * 2.0 ________________ 436 / 10 / 5 - 9 / 2 * 2.5 / 2 ________________ 2. Parameter Mystery, 12 points. Consider the following program. public class Mystery { public static void main(String[] args) { String hear = "bad"; String song = "good"; String good = "hear"; String walk = "talk"; String talk = "feel"; String feel = "walk"; claim(feel, song, feel); claim(good, hear, song); claim(talk, "song", feel); claim(talk, "bad", walk); } public static void claim(String hear, String good, String song) { System.out.println("to " + hear + " the " + song + " is " + good); } } List below the output produced by this program. 3. If/Else Simulation, 12 points. Consider the following method. public static void ifElseMystery(int a, int b) { if (a > b) { a--; b++; } else if (a == b) { a = a + b - 4; } if (a < b) { a = a + 3; } else { b = b + 5; } System.out.println(a + " " + b); } For each call below, indicate what output is produced. Method Call Output Produced ifElseMystery(1, 2); _______________ ifElseMystery(4, 2); _______________ ifElseMystery(3, 2); _______________ ifElseMystery(2, 2); _______________ ifElseMystery(4, 4); _______________ ifElseMystery(8, 6); _______________ 4. While Loop Simulation, 12 points. Consider the following method: public static void mystery(int n) { int x = 1; int y = 2; while (y < n) { if (n % y == 0) { n = n / y; x++; } else { y++; } } System.out.println(x + " " + n); } For each call below, indicate what output is produced: Method Call Output Produced mystery(4); _______________ mystery(5); _______________ mystery(24); _______________ mystery(28); _______________ 5. Assertions, 15 points. You will identify various assertions as being either always true, never true or sometimes true/sometimes false at various points in program execution. The comments in the method below indicate the points of interest. public static int mystery(int x) { if (x <= 0) { x = 42; } int y = 0; // Point A while (x != 1) { // Point B if (x % 2 == 0) { y++; x = x / 2; // Point C } else { x = 3 * x + 1; // Point D } } // Point E return y; } Fill in the table below with the words ALWAYS, NEVER or SOMETIMES. x == 1 x % 2 == 1 y == 0 +---------------------+---------------------+---------------------+ Point A | | | | +---------------------+---------------------+---------------------+ Point B | | | | +---------------------+---------------------+---------------------+ Point C | | | | +---------------------+---------------------+---------------------+ Point D | | | | +---------------------+---------------------+---------------------+ Point E | | | | +---------------------+---------------------+---------------------+ 6. Programming, 15 points. Write a static method called generate that takes a console Scanner as a parameter and that asks a user to pick a number between -5 and 5 inclusive, showing a sequence of random numbers in that range until the user's number comes up and returning how many numbers it generated. The method should construct and use a Random object to generate the numbers. For example, if you execute the following code: Scanner console = new Scanner(System.in); int result = generate(console); you would see an interaction like this: number between -5 and 5? 0 numbers are: 5, 5, -2, -1, -2, 3, -4, -5, 2, 0 came up after 10 tries Notice that the user is prompted for a number and enters 0. The method then generates a random sequence of numbers in that range until the user's number comes up, showing the sequence of numbers. Then it reports and returns how many numbers it generated. The variable result would be set to 10. It is possible that the user's number will be generated immediately, as in: number between -5 and 5? -5 numbers are: -5 came up after 1 tries Here the user enters -5, which is the first value generated, so the method reports that and returns 1. You are to exactly reproduce the format of these logs. 7. Programming, 15 points. Write a static method called digitRange that takes an integer n as a parameter and that returns the greatest difference between two digits of n. In particular, the method should report the largest difference (x - y) where x and y are digits of n. For example, the call digitRange(68437) should return 5 because the largest difference that can be formed using digits from the number is (8 - 3). You may assume that the number passed to the method is greater than or equal to 0. If the method is passed a 1-digit number, it should return 0. Below are more examples of calls and the values that should be returned. Method Value Method Value Call Returned Call Returned --------------------------------- --------------------------------- digitRange(0) 0 digitRange(888) 0 digitRange(5) 0 digitRange(1234) 3 digitRange(26) 4 digitRange(24680) 8 digitRange(42) 2 digitRange(857492) 7 digitRange(725) 5 digitRange(3876254) 6 You are not allowed to use a String to solve this problem. 8. Programming, 9 points. Write a static method called printStripped that takes a string as a parameter and that prints a complete line of output with any comments stripped from the string. Comments are defined to be characters enclosed in the characters "<" and ">". More precisely, text is "normal" until you encounter a "<" character. From that point on the text is considered a comment until you encounter a ">" character, at which point you return to normal text. This definition allows for "<" inside a comment and ">" outside a comment. You may assume that there are no unclosed comments in the string. For example, the following sequence of calls: printStripped("this is plain text"); printStripped("this has a normal comment <right here> to be removed"); printStripped("this has multiple less-than in a comment <<<<<see?>"); printStripped("this > has <comment>greater-than outside a comment >>"); printStripped("<hi>this has <foo> multiple <bar> comments<baz><>."); should produce the following output: this is plain text this has a normal comment to be removed this has multiple less-than in a comment this > has greater-than outside a comment >> this has multiple comments. In solving this problem you may use only methods listed on the cheat sheet.
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011