CSE143X Midterm Fall 2013 Name of Student_______________________________________________________________ Section (e.g., AA)___________________ TA_____________________________________ The 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 10 _____ 7 Programming 10 _____ 8 Arrays 10 _____ 9 Programming 9 _____ ---------------------------- 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 7 of the textbook. You may assume that appropriate import declarations have been included to provide access to standard classes, but you are not allowed to use classes or methods that aren't listed on the cheat sheet. 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. 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 printMultiples that takes an integer n and an integer m as parameters and that prints a complete line of output reporting the first m multiples of n. For example, the following calls: printMultiples(3, 5); printMultiples(7, 3); should produce this output: The first 5 multiples of 3 are 3, 6, 9, 12, 15 The first 3 multiples of 7 are 7, 14, 21 Notice that the multiples are separated by commas. You are to exactly reproduce this format. Also notice the order of the parameters: the first parameter is the base number and the second parameter is the number of multiples to generate. You may assume that the number of multiples you will be asked to generate is greater than or equal to one. 7. 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. 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: Fri Sep 16 16:16:07 PDT 2011