Key to CSE143X Midterm, Fall 2021 handout #11 1. Expression Value ----------------------------------------------- 13 + 2 * 5 / 3 16 2.5 * 2 * 5 / 10 + 1.5 4.0 85 % 10 + 4 % 10 - 17 % 3 7 2 + 3 + "." + (3 + 4) + 2 * 3 "5.76" 482 / 10 / 5 / 2.0 * 2 + 14 / 5 11.0 2. Parameter Mystery. The program produces the following output. a snow and slippery for sleet a storm and snow for sleetsnow a snowsleet and sleetsnow for snowstorm a sun and storm for sunny 3. Method Call Output Produced --------------------------------------- ifElseMystery(2, 5); 4 2 ifElseMystery(5, 5); 10 20 ifElseMystery(6, 3); 3 13 ifElseMystery(4, 4); 8 9 ifElseMystery(3, 8); 5 15 ifElseMystery(9, 5); 6 5 4. Method Call Output Produced --------------------------------------- mystery(4); 2 2 mystery(5); 1 5 mystery(24); 4 3 mystery(28); 3 7 5. x == 1 x % 2 == 1 y == 0 +---------------------+---------------------+---------------------+ Point A | sometimes | sometimes | always | +---------------------+---------------------+---------------------+ Point B | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | sometimes | sometimes | never | +---------------------+---------------------+---------------------+ Point D | never | never | sometimes | +---------------------+---------------------+---------------------+ Point E | always | always | sometimes | +---------------------+---------------------+---------------------+ 6. One possible solution appears below. public static int generate(Scanner console) { System.out.print("number between -5 and 5? "); int target = console.nextInt(); Random r = new Random(); int next = r.nextInt(11) - 5; System.out.print("numbers are: " + next); int count = 1; while (next != target) { next = r.nextInt(11) - 5; System.out.print(", " + next); count++; } System.out.println(); System.out.println("came up after " + count + " tries"); return count; } 7. One possible solution appears below. public static void switchNames(Scanner input) { while (input.hasNextLine()) { String line = input.nextLine(); if (line.equals("name:")) { String line2 = input.nextLine(); int commaIndex = line2.indexOf(","); String last = line2.substring(0, commaIndex); String first = line2.substring(commaIndex + 2); System.out.println(first + " " + last); } else { System.out.println(line); } } } 8. Arrays. One possible solution appears below. public static boolean isAllPairs(int[] list) { if (list.length % 2 == 1) { return false; } for (int i = 1; i < list.length; i += 2) { if (list[i] != list[i - 1]) { return false; } } return true; } 9. Two possible solutions appear below. public static String undouble(String s) { String result = ""; if (s.length() > 0) { char last = s.charAt(0); result += last; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) != last) { result += s.charAt(i); } last = s.charAt(i); } } return result; } public static String undouble(String s) { String result = ""; if (s.length() > 0) { result = s.substring(0, 1); for (int i = 1; i < s.length(); i++) { if (s.charAt(i) != s.charAt(i - 1)) { result += s.charAt(i); } } } return result; }
Stuart Reges
Last modified: Wed Oct 27 14:37:51 PDT 2021