CSE142 Midterm Autumn 2014 Name of Student_______________________________________________________________ Section (e.g., AA)___________________ TA_____________________________________ 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 3 * 4 + 5 * 6 ________________ 23 % 5 - 17 % (16 % 10) ________________ "1" + 2 + 3 * 4 + (5 + 6) ________________ 1.5 * 2 + 20 / 3 / 4.0 + 6 / 4 ________________ 345 / 10 / 3 + 10 / (5 / 2.0) ________________ 2. Parameter Mystery, 12 points. Consider the following program. public class Mystery { public static void main(String[] args) { String foo = "buzz"; String sam = "sue"; String sue = "foo"; String bill = "hope"; String hope = "bill"; say(foo, "bill", sam); say(hope, bill, sue); say(bill, hope, sam); say("sue", "hope", hope); } public static void say(String foo, String sam, String sue) { System.out.println(sam + " wants " + sue + " to " + foo + "."); } } 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++; } if (a <= b) { a = a * 2; b--; } else { b = b * 2; } if (a == b) { a = a - 4; } System.out.println(a + " " + b); } For each call below, indicate what output is produced. Method Call Output Produced ifElseMystery(2, 3); _______________ ifElseMystery(1, 4); _______________ ifElseMystery(5, 4); _______________ ifElseMystery(4, 4); _______________ 4. While Loop Simulation, 12 points. Consider the following method: public static void mystery(int z) { int x = 1; int y = 1; while (z > 1) { x++; y = y * 2; z = z / 2; } System.out.println(x + " " + y); } For each call below, indicate what output is produced. Method Call Output Produced mystery(1); _______________ mystery(5); _______________ mystery(10); _______________ mystery(42); _______________ 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) { int y = 1; int z = 0; // Point A while (x > y) { // Point B z = z + x - y; x = x / 2; // Point C y = y * 2; // Point D } // Point E return z; } Fill in the table below with the words ALWAYS, NEVER or SOMETIMES. x > y z > 0 y % 2 == 0 +---------------------+---------------------+---------------------+ Point A | | | | +---------------------+---------------------+---------------------+ Point B | | | | +---------------------+---------------------+---------------------+ Point C | | | | +---------------------+---------------------+---------------------+ Point D | | | | +---------------------+---------------------+---------------------+ Point E | | | | +---------------------+---------------------+---------------------+ 6. Programming, 15 points. Write a static method called randomWalk that takes a Random object and a target position as parameters and that simulates a random walk. Your method should keep track of a current position that is initialized to 0. It should use the provided Random object to generate numbers to be added to the position that are between -2 and 2 inclusive with all possible values being equally likely. The simulation should end when the position reaches the provided target value and should report the maximum and minimum positions seen. For example, the following call: Random r = new Random(); randomWalk(r, 2); indicates that the goal is 2. It should produce output like the following: goal = 2, pos = 0 adding -1, pos = -1 adding 2, pos = 1 adding 2, pos = 3 adding 1, pos = 4 adding 0, pos = 4 adding -2, pos = 2 min = -1, max = 4 Your method must exactly reproduce the format of this log. 7. Programming, 15 points. Write a static method called checkPrime that takes an integer n as a parameter and that checks how many factors n has, returning true if it is prime (exactly 2 factors) and returning false otherwise. Recall that a factor is a number between 1 and n that goes evenly into n and that prime numbers are those that have exactly two factors (1 is not a prime). So if we make the following call: boolean test = checkPrime(24); The method should produce the following two lines of output: factors of 24 = 1, 2, 3, 4, 6, 8, 12, 24 Total factors = 8 and the variable test would be set to false because this number does not have exactly 2 factors. Below are three other sample calls: checkPrime(1) checkPrime(5) checkPrime(25) ---------------------------------------------------------------------- factors of 1 = 1 factors of 5 = 1, 5 factors of 25 = 1, 5, 25 Total factors = 1 Total factors = 2 Total factors = 3 returns false returns true returns false Notice that for these examples the method returns true only for 5 because it is the only prime. You must exactly reproduce the format of these logs. You may assume that the value passed to your method is greater than 0. 8. Programming, 9 points. Write a static method called undouble that takes a string as a parameter and that returns a new string obtained by replacing every pair of repeated adjacent letters with one of that letter. For example, the String "bookkeeper" has three repeated adjacent letters ("oo", "kk", and "ee"), so undouble("bookkeeper") should return the string "bokeper". Below are more sample calls: Method Value Method Value Call Returned Call Returned --------------------------------- --------------------------------- undouble("odegaard") "odegard" undouble("oops") "ops" undouble("baz") "baz" undouble("foobar") "fobar" undouble("mississippi") "misisipi" undouble("apple") "aple" undouble("carry") "cary" undouble("berry") "bery" undouble("juggle") "jugle" undouble("theses") "theses" undouble("little") "litle" undouble("") "" You may assume that the string is composed entirely of lowercase letters, as in the examples above, and that no letter appears more than two times in a row. But notice that the method might be passed an empty string, in which case it returns an empty string. You may use only the string methods included on the cheat sheet. 9. Bonus, 1 point. Please indicate in the box below the score you expect to receive on this midterm. Please give us your best estimate. Any number between 0 and 101 will receive the bonus point. +---------+ I expect to get a score of | | on this test +---------+
Stuart Reges
Last modified: Tue Jan 20 08:21:33 PST 2015