CSE142 Midterm Winter 2005 Name of Student_______________________________________________________________ Section (e.g., AA)___________________ TA_____________________________________ Exam time (9:30, 11:30)_______________ Seat #_________________________________ This is an open-book/open-note exam. Space is provided for your answers. Use the backs of pages if necessary. The exam is divided into seven questions with the following points: # Problem Area Points Score --------------------------------------------- 1 Expressions 10 _____ 2 Parameter Mystery 20 _____ 3 Simulation 15 _____ 4 Assertions 15 _____ 5 Programming 15 _____ 6 Programming 15 _____ 7 Programming 10 _____ ---------------------------- Total 100 _____ The exam is not graded on style and you do not need to include comments. You may use a calculator to do simple arithmetic, although you are not allowed to program the calculator or to run a stored program. 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 through the front door. 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 * 4 * 3/12 + 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, 20 points. Consider the following program. public class Mystery { public static void main(String[] args) { String a = "king"; String b = "two"; String c = "queen"; String two = "five"; sentence(a, b, c); sentence("b", c, c); sentence(two, "two", a); sentence(c, a, b); sentence(two, "queen", b); } public static void sentence(String b, String c, String a) { System.out.println("a " + c + " and a " + a + " beats a " + b); } } List below the output produced by this program. 3. Simulation, 15 points. Consider the following method: public static int mystery(int x, int y) { int z = 0; while (y > 0) { z = z + x; x = x + y; y--; } return z; } For each call below, indicate what value is returned: Method Call Value Returned mystery(6, 0) _______________ mystery(8, 1) _______________ mystery(3, 3) _______________ mystery(4, 2) _______________ mystery(1, 4) _______________ 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 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 | | | | +---------------------+---------------------+---------------------+ 5. Programming, 15 points. Write a method numUnique that takes three integers as parameters and that returns the number of unique integers among the three. For example, if the following call is made: numUnique(18, 3, 4) the method should return 3 because the parameters represent 3 different numbers (the values 18, 3 and 4). By contrast, the following call: numUnique(6, 6, 6) would return 1 because there is only 1 unique number among the three parameters (the value 6). The values passed to your method might appear in any order whatsoever, so you cannot assume that duplicate values would appear together. For example, if the following call is made: numUnique(7, 31, 7) the method should return 2 because there are 2 unique numbers among the parameters (the values 7 and 31). Write your solution to numUnique below. 6. Programming, 15 points. Write a method printRange that takes two integers as parameters, a low and a high, and that prints out all integers in the range from low to high inclusive inside square brackets and separated by commas. For example, the call: printRange(5, 10); should produce the following output: [5,6,7,8,9,10] Your method should produce a complete line of output. If low and high are equal, the output should be a list with just one number. If low is larger than high, the output should be an empty list (with just the square brackets). Your method should also work properly for negative integers. For example, the following series of calls: printRange(3, 3); printRange(10, 1); printRange(-10, -5); printRange(-5,-10); should produce the following four lines of output: [3] [] [-10,-9,-8,-7,-6,-5] [] Write your solution to printRange below. 7. Programming, 10 points. Write a method numWords that takes a String as a parameter and that returns the number of words in the String. By definition, words are separated by one or more spaces. For example: numWords("how many words here?") should return 4. Notice that words can contain punctuation marks. Any non-empty sequence of non-space characters can be a word. There might be spaces at the beginning or end of the String. For example: numWords(" how about merry-go-round and !&$%--$$!!*() ") should return 5. You may not construct any other objects to solve this problem (e.g., you can't use a Scanner or tokenizer). You may assume that the String has no other whitespace characters such as tabs or newline characters. Your method has to pay attention only to spaces to decide how many words there are. Write your solution to numWords below.
Stuart Reges
Last modified: Fri Sep 16 15:35:41 PDT 2011