CSE143X Sample Midterm, Fall 2018 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 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 x = "lemonade"; String y = "ants"; String z = "picnic"; String picnic = "y"; String ants = x + picnic; picnic(x, y, z); picnic(y, picnic, "ants"); picnic(y + z, y + picnic, ants); picnic = "sunny"; picnic(picnic, "today", x); } public static void picnic(String z, String y, String x) { System.out.println(x + " and " + z + " like " + y); } } 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 % 2 == 0) { a++; b--; } else if (a % 2 == 1) { b = 0; } if (b == 0 && b != a) { a = a + 2; b = a - 2; } System.out.println(a + " " + b); } For each call below, indicate what output is produced. Method Call Output Produced ifElseMystery(5, 20); _______________ ifElseMystery(42, 42); _______________ ifElseMystery(6, 1); _______________ ifElseMystery(2, 0); _______________ ifElseMystery(7, 10); _______________ ifElseMystery(4, 4); _______________ 4. While Loop Simulation, 12 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(184); _______________ mystery(8239); _______________ 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(Scanner console) { int y = 42; int z = 0; // Point A while (y != 0) { // Point B y = console.nextInt(); if (y % 2 == 0) { z++; // Point C y--; } // Point D y--; } // Point E System.out.println("z = " + z); } Fill in the table below with the words ALWAYS, NEVER, or SOMETIMES (you may abbreviate them as A, N, or S). y == 0 y % 2 == 0 z == 0 +---------------------+---------------------+---------------------+ Point A | | | | +---------------------+---------------------+---------------------+ Point B | | | | +---------------------+---------------------+---------------------+ Point C | | | | +---------------------+---------------------+---------------------+ Point D | | | | +---------------------+---------------------+---------------------+ Point E | | | | +---------------------+---------------------+---------------------+ 6. Programming, 10 points. Write a static method called printSequenceTo that takes a target value as a parameter and that prints terms from a particular numerical sequence until they add up to a value greater than or equal to the target and that returns the number of terms that were included. For example, if the following calls are made: int n1 = printSequenceTo(3.0); int n2 = printSequenceTo(5.5); The following output should be produced: 1/2 + 2/3 + 3/4 + 4/5 + 5/6 = 3.5500000000000003 1/2 + 2/3 + 3/4 + 4/5 + 5/6 + 6/7 + 7/8 + 8/9 = 6.171031746031746 The variable n1 is set to 5 because it took 5 terms from the sequence to get a sum that is at least 3.0. The variable n2 would be set to 8 because it took 8 terms to get a sum that is at least 5.5. You are to exactly reproduce the format of this output. You may assume that the target is greater than 0. Notice that the sum is not rounded. 7. File Processing, 10 points. Write a static method called switchData that takes as a parameter a Scanner containing an input file of labeled data and that prints the same information with successive pairs of numbers on each line switched in order. Each line of the input file will consist of a label followed by a sequence of integers. For example, if a variable called input refers to the following file: Karen 1 2 3 4 5 6 Sam 38 14 79 4 -3 42 2 4 6 8 12 extras anon 15 then the call: switchData(input); would produce the following output: Karen 2 1 4 3 6 5 Sam 14 38 4 79 -3 42 4 2 8 6 12 extras anon 15 The first line of the file has the label "Karen" followed by 6 integers. Notice that the first pair of integers (1, 2) has been switched (2, 1), and the second pair of integers (3, 4) has been switched (4, 3), and so on. This first example involved sequential integers to make the switching more obvious, but this won't always be the case. You also shouldn't assume that you have an even number of integers. If there is an odd number of integers, as in the second line of input, then the final value should not be moved (the -3 appears at the end in both input and output). There will always be a one-word label (possibly a number as in the example of "42"), but the list of integers might be empty, in which case the method simply prints the label on a line by itself (as in the example of "extras"). You may assume that the input is legal (a sequence of lines each with a one-word label followed by 0 or more integer values). You may not construct any extra data structures to solve this problem other than Scanner objects. 8. Arrays, 10 points. Write a static method called minToFront that takes an array of integers as a parameter and that moves the minimum value in the list to the front by swapping its position with whatever is currently at the front of the list. For example, if a variable called list stores the following values: [3, 8, 92, 4, 2, 17, 9] and you make the following call: minToFront(list); The value 2 is the minimum, so the list should store the following values after the call: [2, 8, 92, 4, 3, 17, 9] Notice that the value 3 which used to be at the front of the list is now at index 4 where the value 2 was before. If there is more than one occurrence of the minimum value, your method should move the first occurrence to the front of the list. If the minimum value is already at the front of the array or if the array is empty, then the array should be unchanged after the method executes. You may not construct any extra data structures to solve this problem (not even a string). 9. Programming, 9 points. Write a static method called isMatch that takes a pattern string and a target string as parameters and that returns whether or not the given target matches the pattern. Patterns can contain special wildcard characters dot (".") and star ("*"). If a pattern does not contain any wildcards, then the target has to be the same string, as in isMatch("and", "and"). A dot can match any single character. For example, the pattern "a.." matches any 3-letter string beginning with the letter "a". A star can match any sequence of characters (including no characters). For example, the pattern "a*t" matches any string that begins with "a" and ends with "t", including "at". There will be at most one star in any given pattern, although a pattern can contain several dots and a star. Below are examples of patterns and matching strings (note that your method compares a pattern against a single string, not a list of strings). Pattern Matching strings --------- ----------------------------------------------------- "hello" hello "..." and, ant, but, cat, cow, hat, sat, tap, ten, the, tot "a..." atom, army, aunt, aura ".a.." bats, task, yard, saga, lava "...a" tuna, soda, coma, aura, saga, lava "....th" growth, zenith, health "a*" a, an, at, and, ant, atom, aunt, apple, army, aura "t*t" tot, that, trot, tiniest "the*" the, then, there, therefore, thermal, thespians ".a*a" saga, lava, saliva, tarantula, nausea "t.e.p*" twerp, trespass, thespians ".o*e." poem, token, wolves, voucher, toothbrushes You are allowed to create new strings, but otherwise you are not allowed to construct extra data structures to solve this problem (no array, ArrayList, Scanner, etc). You are limited to the string methods listed on the cheat sheet (otherwise this problem can be solved in one line of code). You can receive up to 4 points for a solution that handles the dot wildcard without handling the star wildcard. If you wish to pursue this option, please mark the box below: +---+ | | I agree to limit my score to 4 by handling just the dot wildcard +---+
Stuart Reges
Last modified: Mon Oct 15 13:22:09 PDT 2018