CSE142 Sample Midterm handout #14
Winter 2005
1. Expressions, 10 points. For each expression in the left-hand column,
indicate its value in the right-hand column. Be sure to list a constant of
appropriate type (e.g., 7.0 rather than 7 for a double, Strings in quotes).
Expression Value
208 % 10 - 90 % 55 % 10 __________
31 % 8/3 * 1.2 __________
203/10/2.0/4 __________
79 % (14/3) - 6 * 7/3 __________
2 + 3 + "-" + 8 * 2 + 3 * 4 __________
2. Parameter Mystery, 20 points. Consider the following program.
public class Mystery {
public static void main(String[] args) {
String one = "student";
String two = "beer";
String three = "dorm";
int number = 12;
Sentence(two, one, number);
Sentence(three, two, 125);
Sentence(three, one, 250);
Sentence(one, two, 4);
Sentence("classroom", one, 2 * number);
}
public static void Sentence(String one, String two, int number) {
System.out.println(two + "s in the " + one + " = " + number);
}
}
List below the output produced by this program.
3. Simulation, 15 points. Consider the following method:
public static int mystery(int x, int y) {
while (x != y) {
if (x < y)
y /= 10;
else
x /= 10;
}
return x;
}
For each call below, indicate what value is returned:
Method Call Value Returned
mystery(976, 9235) _______________
mystery(348, 34298) _______________
mystery(2974, 2184) _______________
mystery(82, 19) _______________
mystery(2348, 239) _______________
4. Assertions, 15 points. You will identify various assertions as being either
always true, never true or sometimes true/sometimes false at various points
in program execution. The comments in the method below indicate the points
of interest.
public static int mystery(int x) {
int y = 1;
int z = 0;
// Point A
while (x > y) {
// Point B
z += x - y;
x /= 2;
// Point C
y *= 2;
// Point D
}
// Point E
return z;
}
Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.
x > y z > 0 y % 2 == 0
+---------------------+---------------------+---------------------+
Point A | | | |
+---------------------+---------------------+---------------------+
Point B | | | |
+---------------------+---------------------+---------------------+
Point C | | | |
+---------------------+---------------------+---------------------+
Point D | | | |
+---------------------+---------------------+---------------------+
Point E | | | |
+---------------------+---------------------+---------------------+
5. Programming, 15 points. Write a method betOnce that takes a console Scanner
and an integer as parameters and that allows the user to make a bet about
the integer. The bet involves guessing whether a number between 1 and 36 is
in the lower half of the range (1 through 18) or the upper half of the range
(19 through 36). It will also be possible for the number to be 0, in which
case the user always loses. These rules are used in a game called Roulette.
Your method will be passed a Scanner that can be used to read input from the
user and it will be passed the number that the user is betting on. For
example, the main method might look like this:
Scanner console = new Scanner(System.in);
Random r = new Random();
int number = r.nextInt(37);
betOnce(console, number);
Your method should prompt the user for which bet they want to make and
should then report the number and whether the user won or lost. For
example, below is a log of execution where the user enters "1" to bet on the
low range:
Do you want to bet on 1) low or 2) high? 1
The number was 1
You win
Below is a log where the user enters "2" to bet on the high range:
Do you want to bet on 1) low or 2) high? 2
The number was 11
You lose
Your method must exactly reproduce the format of these logs. You may assume
that your method is always passed a number between 0 and 36. Remember that
the user always loses when the number is 0.
Write your solution to betOnce below.
6. Programming, 15 points. Write a method sumTo that takes an integer
parameter n and that returns the sum of the first n reciprocals. In other
words:
sumTo(n) returns (1 + 1/2 + 1/3 + 1/4 + ... + 1/n)
For example, sumTo(2) should return the value 1.5 (1 + 1/2). If sumTo is
passed the value 0, it should return 0.0 as its result. You may assume that
sumTo is never passed a negative value.
Write your solution to sumTo below.
7. Programming, 10 points. Write a method digitSum that takes a non-negative
integer as a parameter and that returns the sum of its digits. For example,
digitSum(20879) should return 26 (2 + 0 + 8 + 7 + 9). You may assume that
the method is passed a value greater than or equal to 0. You may not use a
String to solve this problem; you must solve it using integer arithmetic.
Write your solution to digitSum below.