CSE143X Sample Midterm handout #7 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 * (5 - 2) - 3 - 2 * 2 __________ 4 * 7 % 8 + 132 % 10 + 3 % 4 __________ 27 / 5 / 2 + 3.4 * 2 - 1.1 * 2 __________ 9 + 9 + "9 + 9" + 9 + 9 __________ 19 / 2 / 2.0 + 2.5 * 6 / 2 + 0.5 * 4 __________ 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++; } if (a < b) { a++; } else { b++; } if (a >= b) { b = b - 5; } System.out.println(a + " " + b); } For each call below, indicate what output is produced. Method Call Output Produced ifElseMystery(1, 8); _______________ ifElseMystery(3, 5); _______________ ifElseMystery(4, 5); _______________ 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(2); _______________ mystery(4); _______________ 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 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 | | | | +---------------------+---------------------+---------------------+ 6. Programming, 10 points. Write a static method called quadrant that takes as parameters a pair of double values representing an (x, y) point and that returns the quadrant number for that point. Recall that quadrants are numbered as integers from 1 to 4 with the upper-right quadrant numbered 1 and the subsequent quadrants numbered in a counter-clockwise fashion: ^ y-axis | | | Quadrant 2 | Quadrant 1 | <--------------------+--------------------> x-axis | Quadrant 3 | Quadrant 4 | | | V Notice that the quadrant is determined by whether the x and y coordinates are positive or negative numbers. If a point falls on the x-axis or the y-axis, then the method should return 0. Below are sample calls on the method. Method Value Method Value Call Returned Call Returned ------------------------------- ------------------------------- quadrant(12.4, 17.8) 1 quadrant(0.0, 0.0) 0 quadrant(-2.3, 3.5) 2 quadrant(12.5, 0.0) 0 quadrant(-15.2, -3.1) 3 quadrant(0.0, 2.3) 0 quadrant(4.5, -4.5) 4 7. Programming, 10 points. Write a static method called 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. 8. Arrays, 10 points. Write a static method called isSorted that takes an array of doubles as an argument and that returns true if the list is in sorted (nondecreasing) order and that returns false otherwise. For example, if variables called list1 and list2 store the following values: list1: [16.1, 12.3, 22.2, 14.4] list2: [1.5, 4.3, 4.3, 7.0, 19.5, 25.1, 46.2] then the call isSorted(list1) should return false while the call isSorted(list2) should return true. By definition, an array with fewer than two elements is considered to be sorted. 9. Programming, 9 points. Write a static method called printCompact that takes a string as an argument and that prints a complete line of output with the text of the string in compact form. The idea is that we might have a string that has a lot of extra spaces in it separating words. For example, if you make the call: printCompact(" this string has lots of spaces "); the method should produce this line of output: this string has lots of spaces This line of output has just 5 spaces separating the six words (no spaces at the beginning or the end of the line). If the string passed to your method is empty or contains just spaces, your method should produce a blank line as output. You may assume that the string does not contain tab characters or other whitespace characters that behave like spaces. You are not allowed to use any auxiliary data structures (no String, array, Scanner, etc) and your solution must run in O(n) time where n is the length of the string. You can receive partial credit if your solution requires such a structure or takes longer to execute.
Stuart Reges
Last modified: Mon Oct 14 17:36:19 PDT 2013