CSE142 Midterm Spring 2006 Name of Student_______________________________________________________________ Section (e.g., AA)___________________ TA_____________________________________ Exam time (9:30, 11:30)______________ This is an open-book/open-note exam. Space is provided for your answers. Use the backs of pages if necessary. The exam is divided into seven questions with the following points: # Problem Area Points Score --------------------------------------------- 1 Expressions 10 _____ 2 Parameter Mystery 20 _____ 3 Simulation 15 _____ 4 Assertions 15 _____ 5 Programming 15 _____ 6 Programming 15 _____ 7 Programming 10 _____ ---------------------------- Total 100 _____ The exam is not graded on style and you do not need to include comments. You may use a calculator to do simple arithmetic, although you are not allowed to program the calculator or to run a stored program. 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 through the front door. 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 4 * (2 + 4) - 3 * 5 __________ 54 % 10 + 8 * 3 % 9 __________ 3 * 2 + 4 + "+" + 2 + 3 * 4 __________ 2.3 * 3 + 19 / 5 / 2 + 6.0 / 5 __________ 108 / 20 * 3 / 4 / 2.0 + 1.0 / 2 __________ 2. Parameter Mystery, 20 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("claim", talk, walk); 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. Simulation, 15 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(2); _______________ mystery(4); _______________ mystery(5); _______________ mystery(24); _______________ mystery(28); _______________ 4. 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 void mystery(int x, int y) { int z = 0; // Point A while (x != y) { // Point B z++; if (x > y) { // Point C x = x / 10; } else { // Point D y = y / 10; } } // Point E System.out.println(x + " " + y + " " + z); } Fill in the table below with the words ALWAYS, NEVER or SOMETIMES. x > y z == 0 x == y +---------------------+---------------------+---------------------+ Point A | | | | +---------------------+---------------------+---------------------+ Point B | | | | +---------------------+---------------------+---------------------+ Point C | | | | +---------------------+---------------------+---------------------+ Point D | | | | +---------------------+---------------------+---------------------+ Point E | | | | +---------------------+---------------------+---------------------+ 5. Programming, 15 points. Write a method flip that takes a Random object as a parameter and that prints information about a coin-flipping simulation. Your method should use the Random object to produce a sequence of simulated coin flips, printing whether each flip comes up "heads" or "tails". Each outcome should be equally likely. Your method should stop flipping when you see three heads in a row. It should return the total number of flips. For example, if we construct a Random object and make the following calls; Random r = new Random(); flip(r); flip(r); We expect to get a log of execution like this: heads tails heads heads heads 3 heads in a row after 5 flips heads heads tails heads tails tails heads heads heads 3 heads in a row after 9 flips You must exactly reproduce the format of the log above. Write your solution to flip below. 6. Programming, 15 points. Write a method minHailstoneValue that takes integers n and m as parameters and that returns the minimum value in a hailstone sequence of length m that begins with n. In a hailstone sequence, each value x is followed either by: (3x + 1) if x is odd (x/2) if x is even For example, if we start with 7 and we construct a sequence of length 10, we get: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20 In this case, the minimum value in the sequence is 7 (the number we started with). Therefore, the following call: minHailstoneValue(7, 10) should return 7. If we instead construct a sequence of length 20, we get: 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1 In this case, the minimum value in the sequence is 1. Therefore, the call: minHailstoneValue(7, 20) should return 1. You may assume that both integers passed to your method are greater than 0. Write your solution to minHailstoneValue below. 7. Programming, 10 points. Write a method sameDashes that takes two strings as parameters and that returns whether or not they have dashes in the same places (returning true if they do and returning false otherwise). For example, below are four pairs of strings of equal length that have the same pattern of dashes. Notice that the last pair has no dashes at all. string1: "hi--there-you." "-15-389" "criminal-plan" "abc" string2: "12--(134)-7539" "-xy-zzy" "(206)555-1384" "9.8" To be considered a match, the strings must have exactly the same number of dashes in exactly the same positions. The strings might be of different length. For example, the following calls should each return true: sameDashes("1st-has-more characters", "2nd-has-less") sameDashes("1st-has-less", "2nd-has-more characters") because the strings each have two dashes and they are in the same positions. But the following calls should each return false because the longer string has a third dash where the shorter string does not: sameDashes("1st-has-more-characters", "2nd-has-less") sameDashes("1st-has-less", "2nd-has-more-characters") Write your solution to sameDashes below.
Stuart Reges
Last modified: Fri Sep 16 15:35:21 PDT 2011