Key to CSE142 Midterm, Spring 2017                          handout #13

1.      Expression                              Value

-----------------------------------------------

2 * 6 - (1 + 3) * 2 - 4 * 3                -8

5 % (2 % 6) + 132 % 10 + 4 % 7              7

408 / 40 / 3 - 3 / 2.0 + 0.3 * 2           2.1

1 + 1 + "4 - 2" + 3 * 2 + 8 / 2       "24 - 264"

11 / 2 * 4.0 - 2.5 * 2 / 0.5 + 0.25      10.25

2. Parameter Mystery.  The program produces the following output.

dna: sit yah be riri

fu: sit yah be kenny

yahyah: sit riri be down

humble: sit kenny be fu

3.      Method Call             Output Produced

---------------------------------------

ifElseMystery(12, 12);      13 15

ifElseMystery(4, 5);        5 4

ifElseMystery(11, 5);       11 8

ifElseMystery(2, 13);       1 14

ifElseMystery(3, 20);       1 21

ifElseMystery(7, 7);        8 10

4.      Method Call             Output Produced

---------------------------------------

mystery(4, 4);                1 1

mystery(202, 201);            3 2

mystery(934, 8344);           3 1

mystery(1083, 81);            2 1

5.                y <= 0               x % 2 == 0              z == 0

+---------------------+---------------------+---------------------+

Point A |     sometimes       |      sometimes      |      always         |

+---------------------+---------------------+---------------------+

Point B |       never         |      sometimes      |     sometimes       |

+---------------------+---------------------+---------------------+

Point C |       never         |       never         |      never          |

+---------------------+---------------------+---------------------+

Point D |      sometimes      |       always        |     sometimes       |

+---------------------+---------------------+---------------------+

Point E |       always        |      sometimes      |     sometimes       |

+---------------------+---------------------+---------------------+

6. One possible solution appears below.

public static void generateGrades(Random r, int num) {

for (int i = 2; i <= num; i++) {

}

}

System.out.println();

System.out.println("total points: " + sum);

System.out.println("average grade: " + (double) sum / num);

}

7. One possible solution appears below.

public static boolean testFairCoin(Scanner console) {

int count = 0;

System.out.print("next flip? ");

int userInput = console.nextInt();

while (userInput != 0) {

if (userInput == 1) {

}

count++;

System.out.print("next flip? ");

userInput = console.nextInt();

}

double percent = heads * 100.0 / count;

System.out.println("was heads " + percent + "% of the time");

return percent >= 45.0 && percent <= 55.0;

}

8. One possible solution appears below.

public static String findMatching(String s1, String s2) {

String result = "";

for (int i = 0; i < Math.min(s1.length(), s2.length()); i++) {

if (s1.charAt(i) == s2.charAt(i)) {

result += s1.charAt(i);

} else {

result += ".";

}

}

int rest = Math.abs(s1.length() - s2.length());

for (int i = 0; i < rest; i++) {

result += ".";

}

return result;

}