Key to CSE142 Midterm, Autumn 2017 1. Expression Value ----------------------------------------------- (8 - 3) * 2 - 2 * 3 + 7 11 37 % 10 / 2 * 1.5 + 5 / 2.0 7.0 106 % 20 + 8 % 12 * 2 - 13 % 8 17 13 - 6 + "-" + 13 / 6 + 13 + 6 "7-2136" 7 / 2 * 2.0 + 1.25 * 4 / 2 8.5 2. Parameter Mystery. The program produces the following output. you calls you with person app calls home with screen me calls (person) with (person) app calls screen with home 3. Method Call Output Produced --------------------------------------- ifElseMystery(4, 7); 9 6 ifElseMystery(5, 5); 5 5 ifElseMystery(8, 3); 12 9 ifElseMystery(1, 12); 14 11 ifElseMystery(9, 6); 12 9 ifElseMystery(3, 4); 8 4 4. Method Call Output Produced --------------------------------------- mystery(6); 6 0 mystery(34) 7 1 mystery(721); 1 3 mystery(65289); 3 5 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 void giveProblems(Scanner console, int numProblems) { Random r = new Random(); int numRight = 0; for (int i = 1; i <= numProblems; i++) { int x = r.nextInt(12) + 1; int y = r.nextInt(12) + 1; System.out.print(x + " * " + y + " =? "); int answer = x * y; int response = console.nextInt(); if (response == answer) { System.out.println("correct"); numRight++; } else { System.out.println("incorrect...the answer was " + answer); } } System.out.println(numRight + " of " + numProblems + " correct"); } 7. One possible solution appears below. public static int printSequenceTo(double value) { double sum = 0.5; System.out.print("1/2"); int n = 1; while (sum < value) { n++; System.out.print(" + " + n + "/" + (n + 1)); sum = sum + (double) n / (n + 1); } System.out.println(" = " + sum); return n; } 8. Two possible solutions appear below. public static String acronym(String s) { String result = ""; if (s.charAt(0) != ' ') { result += s.charAt(0); } for (int i = 1; i < s.length(); i++) { if (s.charAt(i - 1) == ' ' && s.charAt(i) != ' ') { result += s.charAt(i); } } return result.toUpperCase(); } public static String acronym2(String s) { boolean inWord = false; s = s.toUpperCase(); String result = ""; for (int i = 0; i < s.length(); i++) { char ch = s.charAt(i); if (ch == ' ') { inWord = false; } else if (!inWord) { inWord = true; result += ch; } } return result; }