Key to CSE142 Sample Midterm handout #17 Fall 2004 1. Expression Value --------------------------------------------- 2.0 * 3/4 + 1 2.5 (11 % 3 + 1.0)/2 1.5 2.4 + 11 % 7/3 3.4 3 + 2 + "-" + 4 * 2 + 5 5-85 6.8/2 + 21/2/(5 - 3) 8.4 2. Parameter Mystery. The program produces the following output. Many a student in the computer of fred Many a computer in the department of major Many a department in the honor of fred Many a baz in the bar of foo Many a major in the department of computer 3. Method Call Value Returned -------------------------------------- mystery(1) 1 mystery(3) 2 mystery(4) 3 mystery(5) 5 mystery(6) 8 4. y < x y == 0 count > 0 +---------------------+---------------------+---------------------+ Point A | sometimes | sometimes | never | +---------------------+---------------------+---------------------+ Point B | always | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point C | always | always | always | +---------------------+---------------------+---------------------+ Point D | sometimes | sometimes | sometimes | +---------------------+---------------------+---------------------+ Point E | never | sometimes | sometimes | +---------------------+---------------------+---------------------+ 5. One possible solution appears below. public static void repeat(String s, int n) { System.out.print("["); if (n > 0) { System.out.print(s); for (int i = 2; i <= n; i++) System.out.print(", " + s); } System.out.println("]"); } 6 One possible solution appears below. public static double tax(double salary) { if (salary <= 7150) return 0.1 * salary; else if (salary < 29050) return 715 + 0.15 * (salary - 7150); else if (salary < 70350) return 4000 + 0.25 * (salary - 29050); else return 14325 + 0.28 * (salary - 70350); } 7. One possible solution appears below. public static int log2(int n) { int power = 0; while (n > 1) { n /= 2; power++; } return power; }
Stuart Reges
Last modified: Sun Nov 7 22:00:37 PST 2004