Key to CSE
142 Midterm, Summer 2017
Problem
#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.0 * 3 / 2
25.5
Problem
#2:
bill wants sue to buzz.
hope wants foo to billbuzz.
sam wants sue to hope.
hope wants sam to sue.
Problem
#3:
Method Call
Output Produced
---------------------------------------
ifElseMystery(5, 15); 7 15
ifElseMystery(12, 5); 12 1
ifElseMystery(7, 8); 8 9
ifElseMystery(2, 3); 4 -2
ifElseMystery(1, -2); 2 -7
ifElseMystery(1, 1); 2 -4
Problem
#4:
Method Call
Output Produced
---------------------------------------
mystery(8);
1 8
mystery(32);
2 5
mystery(184);
3 13
mystery(8239);
4 22
Problem
#5:
next == 0
a > 0
b >= next
+---------------------+---------------------+---------------------+
Point A | sometimes
| never
| sometimes
|
+---------------------+---------------------+---------------------+
Point B | never
| sometimes
| sometimes
|
+---------------------+---------------------+---------------------+
Point C | never
| sometimes
| always
|
+---------------------+---------------------+---------------------+
Point D | never
| always
| always
|
+---------------------+---------------------+---------------------+
Point E | always
| sometimes |
sometimes
|
+---------------------+---------------------+---------------------+
Problem
#6: One possible solution appears below.
public
static void threeDigitPrime(Random r) {
int n = r.nextInt(900)
+ 100;
System.out.print("factors
of " + n + " = 1");
int count = 1;
for (int i
= 2; i <= n; i++) {
if (n % i == 0) {
System.out.print(", " + i);
count++;
}
}
System.out.println();
System.out.println("total factors = " + count);
if (count == 2) {
System.out.println(n + " is prime");
} else {
System.out.println(n + " is not prime");
}
}
Problem #7:
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;
}
Problem
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;
}