CSE142 Sample Midterm handout #16
Fall 2004
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).
Expression Value
2.0 * 3/4 + 1 __________
(11 % 3 + 1.0)/2 __________
2.4 + 11 % 7/3 __________
3 + 2 + "-" + 4 * 2 + 5 __________
6.8/2 + 21/2/(5 - 3) __________
2. Parameter Mystery, 20 points. Consider the following program.
public class Mystery {
public static void main(String[] args) {
String major = "fred";
String fred = "computer";
String computer = "department";
String department = "student";
String student = "major";
sentence(major, fred, department);
sentence(student, computer, fred);
sentence("fred", "honor", computer);
sentence("foo", "bar", "baz");
sentence(fred, computer, student);
}
public static void sentence(String major, String fred, String foo) {
System.out.println("Many a " + foo + " in the " + fred +
" of " + major);
}
}
List below the output produced by this program.
3. Simulation, 15 points. Consider the following method:
public static int mystery(int n) {
int x = 1;
int y = 1;
while (n > 2) {
y += x;
x = y - x;
n--;
}
return y;
}
For each call below, indicate what value is returned:
Method Call Value Returned
mystery(1) _______________
mystery(3) _______________
mystery(4) _______________
mystery(5) _______________
mystery(6) _______________
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(Scanner console, int x) {
int y = console.nextInt();
int count = 0;
// Point A
while (y < x) {
// Point B
if (y == 0) {
count++;
// Point C
}
y = console.nextInt();
// Point E
}
// Point E
return count;
}
Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.
y < x y == 0 count > 0
+---------------------+---------------------+---------------------+
Point A | | | |
+---------------------+---------------------+---------------------+
Point B | | | |
+---------------------+---------------------+---------------------+
Point C | | | |
+---------------------+---------------------+---------------------+
Point D | | | |
+---------------------+---------------------+---------------------+
Point E | | | |
+---------------------+---------------------+---------------------+
5. Programming, 15 points. Write a method repeat that takes a string and an
integer as parameters and that produces a line of output with that number of
the string inside square brackets and separated by commas. For example, the
following calls:
repeat("Joe", 3);
repeat("<>", 15);
Should produce the following two lines of output:
[Joe, Joe, Joe]
[<>, <>, <>, <>, <>, <>, <>, <>, <>, <>, <>, <>, <>, <>, <>]
Notice that you must use commas to separate the different occurrences of the
string and that you must enclose the entire list in square brackets. The
method might be asked to produce 0 occurrences of the string, in which case
it should produce a line with a set of empty square brackets. For example,
the following call:
repeat("ha ha", 0);
should produce the following output:
[]
You may assume that your method is never asked to produce a negative number
of occurrences. The method does not return a value, it just produces
output. Your method should exactly reproduce the format above. Write your
solution to repeat below.
6. Programming, 15 points. Write a method tax that takes a salary as a
parameter and that returns the amount of federal tax you would owe if you
make that salary. The tax is computed based on your tax bracket as
described in the table below. You use the first two columns to find the
appropriate bracket. Once you know which row of the table to use, start
with the "flat amount" and add the "+%" of the amount over the amount listed
in the final column. For example, if your income is $50,000, then you use
the third row of the table and compute the tax as $4,000 plus 25% of the
amount over $29,050.
Over But not over Flat amount +% Of excess over
-------------------------------------------------------------
$0 $7,150 $0 10% $0
$7,150 $29,050 $715 15% $7,150
$29,050 $70,350 $4,000 25% $29,050
$70,350 unlimited $14,325 28% $70,350
You may assume that your method is passed a value of type double and you
should return a double. Don't worry about whether or not the tax works out
to an even amount of money (dollars and cents). Just compute the answer as
a double using the formula. You may assume that your method is passed a
value greater than or equal to 0. Write your solution to tax below.
7. Programming, 10 points. Write a method log2 that takes an integer n as a
parameter and that returns the floor of the logarithm to the base 2 of n.
In other words, log2(n) should return the largest value p such that 2^p is
less than or equal to n. Below are some examples.
n log2(n) because
-----------------------------------------------------------------
3 1 2^1 is the largest power of 2 that is <= 3
4 2 2^2 is the largest power of 2 that is <= 4
5 2 2^3 is the largest power of 2 that is <= 5
6 2 2^3 is the largest power of 2 that is <= 6
8 3 2^3 is the largest power of 2 that is <= 8
9 3 2^4 is the largest power of 2 that is <= 9
65 6 2^6 is the largest power of 2 that is <= 65
130 7 2^7 is the largest power of 2 that is <= 135
You are not allowed to call any of the methods of the Math class to solve
this problem. You may assume that log2 is passed a value greater than or
equal to 0. Write your solution to log2 below.