Key to CSE142 Sample Midterm, Fall 2017 1. Expression Value ----------------------------------------------- 5 * 6 - (4 + 3) * 2 - 2 * 3 10 208 / 20 / 4 + 12 / 10.0 + 0.4 * 2 4.0 8 - 2 + "8 - 2" + 8 * 2 + 8 "68 - 2168" 4 * 5 % 6 + 297 % 10 + 4 % 8 13 13 / 2 * 3.0 + 5.5 * 3 / 2 26.25 2. Parameter Mystery. The program produces the following output. table and chair like notes notes and notes like x notesx and notestable like notesx chair and boring like today 3. Method Call Output Produced --------------------------------------- ifElseMystery(6, 5); 5 15 ifElseMystery(4, 6); 7 5 ifElseMystery(9, 5); 7 15 ifElseMystery(3, 6); 5 5 ifElseMystery(2, 7); 8 6 ifElseMystery(1, 3); 3 13 4. Method Call Output Produced --------------------------------------- mystery(5); 0 0 mystery(28); 2 82 mystery(346); 2 64 mystery(265408); 3 804 5. x > 0 x == 0 y == 0 +---------------------+---------------------+---------------------+ Point A | always | never | always | +---------------------+---------------------+---------------------+ Point B | always | never | sometimes | +---------------------+---------------------+---------------------+ Point C | never | never | never | +---------------------+---------------------+---------------------+ Point D | never | sometimes | never | +---------------------+---------------------+---------------------+ Point E | never | always | never | +---------------------+---------------------+---------------------+ 6. One possible solution appears below. public static void printSum(int n, int low, int high) { System.out.println("sum " + n + " numbers " + low + " to " + high); Random r = new Random(); int next = r.nextInt(high - low + 1) + low; int max = next; int sum = next; System.out.print(next); for (int i = 2; i <= n; i++) { next = r.nextInt(high - low + 1) + low; System.out.print(" + " + next); sum = sum + next; if (sum > max) { max = sum; } } System.out.println(" = " + sum); System.out.println("max = " + max); } 6. Programming. Two possible solutions appear below: public static double tallyVotes(Scanner input) { System.out.print("vote? "); String vote = input.next(); double result = 0; int total = 0; while (!vote.equals("q")) { if (vote.equals("y")) { result++; } total++; System.out.print("vote? "); vote = input.next(); } result = result / total * 100; System.out.println("total votes = " + total); System.out.println("result = " + result + "%"); return result; } public static double tallyVotes(Scanner input) { String vote = ""; int yes = 0; int no = 0; while (!vote.equals("q")) { System.out.print("vote? "); vote = input.next(); if (vote.equals("y")) { yes++; } else if (vote.equals("n")) { no++; } } int total = yes + no; double result = 100.0 * yes / total; System.out.println("total votes = " + total); System.out.println("result = " + result + "%"); return result; } 8. 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: Fri Oct 27 06:37:23 PDT 2017