Key to CSE143X Midterm, Fall 2017 1. Expression Value ----------------------------------------------- 5 + 2 * 4 / 3 + 5 12 5 + 5 + "23.0" + 5 + 2 * 5 1023.0510 !(5 > 2 && -2 > 2) || 5 / 2 == 0 true 15 % 9 % 4 + 4 % 6 % 3 3 12 / 5 / 2.0 + 2 * 4 9.0 2. Parameter Mystery. The program produces the following output. say coke not pepsi or pop say soda not soda or pepsi say pepsi not koolaid or pop say say not pepsi or pepsi 3. Method Call Output Produced --------------------------------------- ifElseMystery(3, 1); 13 1 2 ifElseMystery(-2, 6); -2 16 7 ifElseMystery(5, -1); 5 9 5 ifElseMystery(1, 2); 1 12 4 ifElseMystery(5, 7); 15 2 5 ifElseMystery(8, -2); 18 -2 5 4. Method Call Output Produced --------------------------------------- mystery(2, 10); 10, 5, 1 mystery(2, 20); 20, 10, 3 mystery(3, 600); 600, 200, 50, 10 mystery(5, 100); 100, 20, 3, 0 5. flip == 0 heads == 0 flip > heads +---------------------+---------------------+---------------------+ Point A | never | always | always | +---------------------+---------------------+---------------------+ Point B | sometimes | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | always | never | never | +---------------------+---------------------+---------------------+ Point D | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point E | always | never | never | +---------------------+---------------------+---------------------+ 6. One possible solution appears below. public static void lucky(int max) { Random r = new Random(); int numLess = 0; int rolls = 0; int roll = r.nextInt(6) + 1; System.out.print(roll); while (numLess < 4) { rolls++; if (roll <= max) { numLess++; } else { numLess = 0; } if (numLess < 4) { roll = r.nextInt(6) + 1; System.out.print(", " + roll); } } System.out.println(); System.out.println("Finished after " + rolls + " rolls."); } 7. One possible solution appears below. public static void analyzeStocks(Scanner input) { while (input.hasNextLine()) { String line = input.nextLine(); Scanner tokens = new Scanner(line); String name = tokens.next(); double profit = 0; while (tokens.hasNext()) { String symbol = tokens.next(); double buy = tokens.nextDouble(); double sell = tokens.nextDouble(); profit += (sell - buy); } System.out.println(name + ": total profit = $" + profit); } } 8. One possible solution appears below. public static boolean sameGap(int[] numbers) { if (numbers.length < 2) { return true; } int target = Math.abs(numbers[0] - numbers[1]); for (int i = 1; i < numbers.length - 1; i++) { int gap = Math.abs(numbers[i] - numbers[i + 1]); if (gap != target) { return false; } } return true; } 9. Two possible solutions appear below. public static void removeZeros(int[] list) { for (int i = list.length - 2; i >= 0; i--) { if (list[i] == 0) { for (int j = i; j < list.length - 1; j++) { list[j] = list[j + 1]; } list[list.length - 1] = 0; } } } public static void removeZeros(int[] list) { int numNonZero = 0; for (int i = 0; i < list.length; i++) { if (list[i] != 0) { list[numNonZero] = list[i]; numNonZero++; } } for (int i = numNonZero; i < list.length; i++) { list[i] = 0; } }