CSE142 Sample Midterm handout #15 Autumn 2005 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 16/3 + 3.2 * 2 __________ 8/7 + "5/4" + 8/3 __________ 88 % 10 % 3 * 16/10 __________ 29/3/2/4.0 + 3.6 * 2 __________ 1.4 + (3 + 2 * 6)/(8 - 14/3) * 2.2 __________ 2. Parameter Mystery, 20 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"; String work = "john"; statement(mary, john, fun); statement(george, work, john); 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. Simulation, 15 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(72); _______________ mystery(184); _______________ mystery(8239); _______________ 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(Scanner console) { int x = 0; int y = 1; int next = console.nextInt(); // Point A while (next != 0) { // Point B y = y * next; if (next < 0) { x++; // Point C } next = console.nextInt(); // Point D } // Point E System.out.println(y + " " + x); } Fill in the table below with the words ALWAYS, NEVER or SOMETIMES. next < 0 y > 0 x > 0 +---------------------+---------------------+---------------------+ Point A | | | | +---------------------+---------------------+---------------------+ Point B | | | | +---------------------+---------------------+---------------------+ Point C | | | | +---------------------+---------------------+---------------------+ Point D | | | | +---------------------+---------------------+---------------------+ Point E | | | | +---------------------+---------------------+---------------------+ 5. Programming, 15 points. Write a method rollDoubles that takes a Random object as a parameter and that prints information about a dice simulation. The method uses the Random object to simulate the rolling of two dice until doubles comes up (i.e., until the two numbers that come up are the same). Assume we are using standard dice that have six sides numbered 1 through 6. Your method should produce a sequence of simulated rolls, printing each roll, until the two numbers rolled match (doubles). The numbers 1 through 6 should be equally likely to appear for any given roll. Your method should also report the total number of rolls it took to reach doubles. For example, if the following two calls are made after constructing a Random object: Random r = new Random(); rollDoubles(r); rollDoubles(r); We expect to get a log of execution like this: Next roll = 2, 6 Next roll = 3, 1 Next roll = 5, 2 Next roll = 1, 2 Next roll = 2, 2 Doubles after 5 rolls Next roll = 2, 6 Next roll = 1, 4 Next roll = 6, 2 Next roll = 3, 3 Doubles after 4 rolls You must exactly reproduce the format of the log above (including the blank lines). Write your solution to rollDoubles below. 6. Programming, 15 points. Write a method before that takes as parameters two month/day combinations and that returns whether or not the first date comes before the second date (true if the first month/day comes before the second month/day, false if it does not). The method will take four integers as parameters that represent the two month/day combinations. The first integer in each pair represents the month and will be a value between 1 and 12 (1 for January, 2 for February, etc, up to 12 for December). The second integer in each pair represents the day of the month (a value between 1 and 31). One date is considered to come before another if it comes earlier in the year. For example, the call: before(6, 3, 9, 20) should return true because June 3rd (6/3) comes before September 20th (9/20). By contrast, the call: before(10, 1, 2, 25) should return false because October 1st (10/1) comes after February 25th (2/25). If the same date is passed twice, your method should return false. For example: before(8, 15, 8, 15) should return false because August 15th (8/15) does not come before August 15th (8/15). You may assume that your method is passed values that represent legal dates. Write your solution to before below. 7. Programming, 10 points. Write a method weave that takes two integers as parameters and that returns the result of weaving their digits together to form a single number. 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 of 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. Write your solution to weave below.
Stuart Reges
Last modified: Fri Oct 28 16:49:16 PDT 2005