CSE142 Midterm Autumn 2013 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 are not allowed to access any other paper during the exam (not even scratch paper). Use the backs of the pages of this test if necessary. Anyone caught with extra paper will lose at least 10 points. 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 13 + 2 * 5 / 3 ________________ 2.5 * 2 * 5 / 10 + 1.5 ________________ 85 % 10 + 4 % 10 - 17 % 3 ________________ 2 + 3 + "." + (3 + 4) + 2 * 3 ________________ 482 / 10 / 5 / 2.0 * 2 + 14 / 5 ________________ 2. Parameter Mystery, 12 points. Consider the following program. public class Mystery { public static void main(String[] args) { String john = "skip"; String mary = "george"; String george = "mary"; String fun = "drive"; statement(mary, john, fun); statement(fun, "george", "work"); statement(george, mary, john); statement(george, "john", "dance"); } public static void statement(String mary, String john, String fun) { System.out.println(john + " likes to " + fun + " with " + mary); } } 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) { b--; } else if (a < b) { a++; } else { b = b + 5; } if (a == b) { a = a + 2; } System.out.println(a + " " + b); } For each call below, indicate what output is produced. Method Call Output Produced ifElseMystery(14, 14); _______________ ifElseMystery(4, 5); _______________ ifElseMystery(10, 5); _______________ ifElseMystery(2, 8); _______________ 4. While Loop Simulation, 12 points. Consider the following method: public static void mystery(int y) { int x = 0; int z = 0; while (y > 0) { x++; z = z + y % 10; y = y / 10; } System.out.println(x + " " + z); } For each call below, indicate what output is produced. Method Call Output Produced mystery(8); _______________ mystery(32); _______________ mystery(184); _______________ mystery(8239); _______________ 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(Scanner console) { int prev = 0; int count = 0; int next = console.nextInt(); // Point A while (next != 0) { // Point B if (next == prev) { // Point C count++; } prev = next; next = console.nextInt(); // Point D } // Point E return count; } Fill in the table below with the words ALWAYS, NEVER or SOMETIMES. next == 0 prev == 0 next == prev +---------------------+---------------------+---------------------+ Point A | | | | +---------------------+---------------------+---------------------+ Point B | | | | +---------------------+---------------------+---------------------+ Point C | | | | +---------------------+---------------------+---------------------+ Point D | | | | +---------------------+---------------------+---------------------+ Point E | | | | +---------------------+---------------------+---------------------+ 6. Programming, 15 points. Write a static method called giveProblems that takes a console scanner and an integer n as parameters and that makes up n multiplication problems for the user to solve. Each multiplication problem involves choosing two numbers that are each between 1 and 12 inclusive. The user should be prompted for the answer and should be told whether their answer was correct or incorrect. The method should also report the number of problems solved correctly and the number of problems given. For example, if the following calls are made: Scanner console = new Scanner(System.in); giveProblems(console, 5); We would expect interaction like the following: 10 * 6 =? 60 correct 9 * 9 =? 82 incorrect...the answer was 81 4 * 10 =? 40 correct 3 * 4 =? 13 incorrect...the answer was 12 8 * 5 =? 40 correct 3 of 5 correct In this log, the user entered 5 answers (60, 82, 40, 13, 40). All other text was produced as output by the method. Notice that there is a final line of output indicating the overall results. Your method must exactly reproduce the format of this log. Your method should construct a Random object to use for making up random problems. You may assume that the integer passed to your method is greater than 0 and that the user always enters an integer when prompted. 7. Programming, 15 points. Write a static method called printSequenceTo that takes a target value as a parameter and that prints terms from a particular numerical sequence until they add up to a value greater than or equal to the target and that returns the number of terms that were included. For example, if the following calls are made: int n1 = printSequenceTo(3.0); int n2 = printSequenceTo(5.5); The following output should be produced: 1/2 + 2/3 + 3/4 + 4/5 + 5/6 = 3.5500000000000003 1/2 + 2/3 + 3/4 + 4/5 + 5/6 + 6/7 + 7/8 + 8/9 = 6.171031746031746 The variable n1 is set to 5 because it took 5 terms from the sequence to get a sum that is at least 3.0. The variable n2 would be set to 8 because it took 8 terms to get a sum that is at least 5.5. You are to exactly reproduce the format of this output. Notice that the sum is not rounded. 8. Programming, 9 points. Write a static method called weave that that takes two integers as parameters and that returns the result of weaving their digits together to form a single integer. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to-the-last digit of x followed by the second-to-the-last digit of y. And so on. For example, consider weaving 128 with 394. The last pair of digits in the result should be 84 (because the original numbers end in 8 and 4). The second-to-the-last pair of digits in the result should be 29 (because the second-to-the-last digits of the original numbers are 2 and 9). The third-to-the-last pair of digits in the result should be 13 (because the third-to-the-last digits of the original numbers are 1 and 3). Thus: weave(128, 394) should return 132984. Notice that the order of the arguments is important. The call weave(394, 128) would return 319248. If one of the numbers has more digits than the other, you should imagine that leading zeros are used to make the numbers have equal length. For example, weave(2384, 12) should return 20308142 (as if it were a call on weave(2384, 0012)). Similarly, weave(9, 318) should return 30198 (as if it were a call on weave(009, 318)). You may assume that the numbers passed to weave are non-negative. You may not use Strings to solve this problem; you must solve it using integer arithmetic. 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: Fri Sep 16 16:16:07 PDT 2011