CSE143X Sample Midterm, Autumn 2016 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 5 * 6 - (4 + 3) * 2 - 2 * 3 ________________ 208 / 20 / 4 + 12 / 10.0 + 0.4 * 2 ________________ 8 - 2 + "8 - 2" + (8 - 2) + 8 ________________ 4 * 5 % 6 + 297 % 10 + 4 % 8 ________________ 13 / 2 * 3.0 + 5.5 * 3 / 2 ________________ 2. Parameter Mystery, 12 points. Consider the following program. public class Mystery { public static void main(String[] args) { String he = "we"; String says = "can"; String yes = "he"; String we = "yes"; slogan(we, yes, says); slogan(says, "Barry", "maybe"); slogan(says, he, yes); slogan("can't", "Sarah", "no"); } public static void slogan(String can, String he, String yes) { System.out.println(he + " says " + yes + " we " + can); } } 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 n) { int x = 1; int y = 1; while (n > y) { x++; y = 10 * y + x; } System.out.println(x + " " + y); } For each call below, indicate what output is produced. Method Call Output Produced mystery(0); _______________ mystery(7); _______________ mystery(32); _______________ mystery(256); _______________ 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 (z % 2 == 0) { x = x * 2; // Point C } else { y--; // Point D } } // Point E System.out.println(z); } Fill in the table below with the words ALWAYS, NEVER, or SOMETIMES (you may abbreviate them as A, N, or S). x < y z == 0 z % 2 == 0 +---------------------+---------------------+---------------------+ Point A | | | | +---------------------+---------------------+---------------------+ Point B | | | | +---------------------+---------------------+---------------------+ Point C | | | | +---------------------+---------------------+---------------------+ Point D | | | | +---------------------+---------------------+---------------------+ Point E | | | | +---------------------+---------------------+---------------------+ 6. Programming, 10 points. Write a static method called digitRange that takes an integer n as a parameter and that returns the greatest difference between two digits of n. In particular, the method should report the largest difference (x - y) where x and y are digits of n. For example, the call digitRange(68437) should return 5 because the largest difference that can be formed using digits from the number is (8 - 3). You may assume that the number passed to the method is greater than or equal to 0. If the method is passed a 1-digit number, it should return 0. Below are more examples of calls and the values that should be returned. Method Value Method Value Call Returned Call Returned --------------------------------- --------------------------------- digitRange(0) 0 digitRange(888) 0 digitRange(5) 0 digitRange(1234) 3 digitRange(26) 4 digitRange(24680) 8 digitRange(42) 2 digitRange(857492) 7 digitRange(725) 5 digitRange(3876254) 6 You are not allowed to use a String or other structured object to solve this problem. 7. File Processing, 10 points. Write a static method called printDuplicates that takes as a parameter a Scanner containing an input file and that prints duplicated tokens along with their counts. Your method should examine each line looking for consecutive occurrences of the same token on that line, printing each duplicated token along with how a count of many times it appears consecutively. Non-repeated tokens are not printed. Repetition across multiple lines (such as if a line ends with a given token and the next line starts with the same token) is not considered in this problem. For example, if the input file contains the following text: hello how how are you you you you I I I am Jack's Jack's smirking smirking smirking smirking smirking bow wow wow yippee yippee yo yippee yippee yay yay yay one fish two fish red fish blue fish It's the Muppet Show, wakka wakka wakka your method would produce the following output: how*2 you*4 I*3 Jack's*2 smirking*5 wow*2 yippee*2 yippee*2 yay*3 wakka*3 Notice that the method prints only the repeated tokens; the ones that appear only once in a row are not shown. Your method should include a single space between each reported duplicate token and should respect the line breaks in the original file. For example, a blank line appears in the expected output, corresponding to the fourth line of the file that did not contain any consecutively duplicated tokens. You may assume that each line of the file contains at least 1 token of input. 8. Arrays, 10 points. Write a static method called hasAlternatingParity that returns whether or not an array of integers has alternating parity (true if it does, false otherwise). The parity of an integer is 0 for even numbers and 1 for odd numbers. To have alternating parity, a list would have to alternate between even and odd numbers, as in the following list: [3, 2, 19, 8, 43, 64, 1, 0, 3] If these values are stored in an array called data and we make this call: hasAlternatingParity(data) the method would return true. If the array instead stored these values: [2, 13, 4, 1, 0, 9, 2, 7, 4, 12, 3, 2] it would return false because there are two even numbers in a row (4, 12). By definition, an empty list or a list of one element has alternating parity. You may assume the values in the array are not negative. 9. Programming, 9 points. Write a static method called distributeTokens that takes an array of token counts and a player index and that distributes the tokens for the given player in a particular way. The idea is that a set of players are playing a game where they accumulate tokens and we have an array that keeps track of how many tokens each player has. The method should distribute tokens as follows. All of the tokens for the given player are collected and removed so that they can be distributed to other players. Tokens are distributed one at a time starting with the player that comes after the given player until all tokens have been distributed. If you reach the last player in the array, then distribution of tokens proceeds starting at the front of the array at index 0. For example, suppose that the current token counts are stored in an array called tokens as follows: [3, 2, 5, 10, 1] And suppose we make this call: distributeTokens(tokens, 1); The method is being asked to distribute the tokens of the player at index 1. That player has 2 tokens that are given to the players who come after, leaving you with this array: [3, 0, 6, 11, 1] Suppose that we then make this call: distributeTokens(tokens, 3); The method is being asked to distribute the tokens for the player at index 3. That player has 11 tokens which are distributed one at a time leaving you with these token counts: [5, 2, 8, 2, 4] Notice that the player with index 3 ends up getting two of the tokens being distributed. You may assume that the array is not empty, that all array values are non-negative, and that the player index is a legal index of the array.
Stuart Reges
Last modified: Mon Oct 17 15:16:09 PDT 2016