CSE142 Midterm
Winter 2012
Name of Student_______________________________________________________________
Section (e.g., AA)___________________ TA_____________________________________
This exam is divided into nine questions with the following points:
# Problem Area Points Score
---------------------------------------------
1 Expressions 10 _____
2 Parameter Mystery 12 _____
3 If/Else Simulation 12 _____
4 While Loop Simulation 12 _____
5 Assertions 15 _____
6 Programming 15 _____
7 Programming 15 _____
8 Programming 9 _____
9 Bonus 1 _____
----------------------------
Total 100 _____
This is a closed-book/closed-note exam. Space is provided for your answers.
There is a "cheat sheet" at the end that you can use as scratch paper or to
write answers. You are not allowed to access any other paper during the exam
(not even scratch paper). Use the backs of the pages of this test if
necessary. Anyone caught with extra paper will lose at least 10 points.
The exam is not graded on style and you do not need to include comments,
although you are limited to the constructs included in chapters 1 through 5 of
the textbook.
You are allowed to abbreviate "Always", "Never," and "Sometimes" as "A", "N",
and "S" for the assertions question, but you should otherwise NOT use any
abbreviations on the exam.
You are NOT to use any electronic devices while taking the test, including
calculators. Anyone caught using an electronic device will receive a 10 point
penalty.
Do not begin work on this exam until instructed to do so. Any student who
starts early or who continues to work after time is called will receive a 10
point penalty.
If you finish the exam early, please hand your exam to the instructor and exit
quietly.
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
4 * (2 + 4) - 3 * 5 __________
54 % 10 + 8 * 3 % 9 __________
3 * 2 + 4 + "+" + 2 + 3 * 4 __________
2.3 * 3 + 19 / 5 / 2 + 6.0 / 5 __________
108 / 20 * 3 / 4 / 2.0 + 1.0 / 2 __________
2. Parameter Mystery, 12 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);
}
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. If/Else Simulation, 12 points. Consider the following method.
public static void ifElseMystery(int a, int b) {
int c = 2;
if (a + c < b) {
c = c + 8;
} else {
b = b + 10;
}
if (a + c < b) {
c = c + 8;
} else {
b = b + 10;
}
System.out.println(b + " " + c);
}
For each call below, indicate what output is produced.
Method Call Output Produced
ifElseMystery(4, 15); _______________
ifElseMystery(7, 17); _______________
ifElseMystery(12, 5); _______________
ifElseMystery(16, 8); _______________
4. While Loop Simulation, 12 points. Consider the following method:
public static int mystery(int x, int y) {
int z = 0;
while (y > 0) {
z = z + x;
x = x + y;
y--;
}
return z;
}
For each call below, indicate what value is returned:
Method Call Value Returned
mystery(6, 0) _______________
mystery(8, 1) _______________
mystery(3, 3) _______________
mystery(4, 2) _______________
5. 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 void mystery(Scanner console) {
int x = 0;
int y = 1;
int next = console.nextInt();
// Point A
while (next != 0) {
// Point B
y = y * next;
if (next < 0) {
x++;
// Point C
}
next = console.nextInt();
// Point D
}
// Point E
System.out.println(y + " " + x);
}
Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.
next < 0 next == 0 x > 0
+---------------------+---------------------+---------------------+
Point A | | | |
+---------------------+---------------------+---------------------+
Point B | | | |
+---------------------+---------------------+---------------------+
Point C | | | |
+---------------------+---------------------+---------------------+
Point D | | | |
+---------------------+---------------------+---------------------+
Point E | | | |
+---------------------+---------------------+---------------------+
6. Programming, 15 points. Write a method called hasMidpoint that takes three
integers as parameters and that returns true if one of the numbers is the
midpoint of the other two and that returns false otherwise. A number is
considered the midpoint of two numbers if it appears halfway between them.
For example, 12 is the midpoint of 10 and 14. The numbers might be in any
order, so any one of the three might be the midpoint of the other two.
The table below shows sample calls and the value that should be returned.
Method Value Method Value
Call Returned Call Returned
--------------------------------- ---------------------------------
hasMidpoint(10, 12, 14) true hasMidpoint(14, 10, 12) true
hasMidpoint(12, 10, 14) true hasMidpoint(2, 10, 6) true
hasMidpoint(8, 8, 8) true hasMidpoint(25, 10, -5) true
hasMidpoint(2, 3, 4) true hasMidpoint(2, 3, 5) false
hasMidpoint(3, 1, 3) false hasMidpoint(2, 4, 5) false
hasMidpoint(21, 9, 58) false hasMidpoint(2, 8, 16) false
7. Programming, 15 points. Write a method called flip that takes a Random
object and an integer n as parameters and that prints information about a
coin-flipping simulation. Your method should use the Random object to
produce a sequence of simulated coin flips, printing whether each flip comes
up "heads" or "tails." Each outcome should be equally likely. Your method
should stop flipping when you see n heads in a row (where n is the second
value passed as a parameter to the method).
For example, if we construct a Random object and make the following calls:
Random r = new Random();
flip(r, 2);
flip(r, 4);
We expect to get output like the following:
flips: H T T T T T T H H
flips: H H T H T H T H H H T T T T T H T H H H H
Notice that we abbreviate "heads" as "H" and "tails" as "T." You must
exactly reproduce the format of the log above, although the specific output
produced will vary on different executions because of the use of the Random
object to produce different sequences. You may assume that the integer
passed as a parameter to your method is greater than 0.
8. Programming, 9 points. Write a method called increasingDigits that takes an
integer n as a parameter and that returns true if the digits of n are
strictly increasing and that returns false otherwise. In other words, your
method should determine if the first digit of the number is strictly less
than the second digit, and the second digit is strictly less than the third
digit, and the third digit is strictly less than the fourth digit, and so
on. Your method should return true for a one-digit number.
The table below shows sample calls and the value that should be returned.
Method Value Method Value
Call Returned Call Returned
--------------------------------- ---------------------------------
increasingDigits(0) true increasingDigits(8) true
increasingDigits(18) true increasingDigits(25) true
increasingDigits(39) true increasingDigits(357) true
increasingDigits(1247) true increasingDigits(456789) true
increasingDigits(42) false increasingDigits(33) false
increasingDigits(1232) false increasingDigits(354) false
increasingDigits(543) false increasingDigits(37789) false
You may not use a String or other object to solve this problem. You may
assume that the number passed to your method is not negative, but it might
be 0.
9. Bonus, 1 point. Please indicate in the box below the score you expect to
receive on this midterm. Please give us your best estimate. Any number
between 0 and 101 will receive the bonus point.
+---------+
I expect to get a score of | | on this test
+---------+
CSE142 Midterm Cheat Sheet
Math Method Description
--------------------------------------
Math.abs(n) absolute value of n
Math.max(x, y) maximum of x and y
Math.min(x, y) minimum of x and y
String Method Description
-----------------------------------------------------------------------------
charAt(index) character at a specific index
indexOf(text) index of a particular string (-1 if not found)
length() number of characters in string
substring(start, stop) characters from start (inclusive) to stop (exclusive)
substring(start) characters from start to end of string
toLowerCase() lowercase version of string
toUpperCase() uppercase version of string
equals(text) true if this string is the same as text
Scanner Method Description
------------------------------------------------------
nextInt() reads/returns input as int
nextDouble() reads/returns input as double
next() reads/returns input as String
Random Method Description
---------------------------------------------------------------------------
nextInt(n) returns a random int between 0 and (n-1) inclusive
Syntax Examples:
for (int i = 1; i <= n; i++) {
System.out.println("****");
}
if (n < 0) {
System.out.println("negative");
}
if (n < 0) {
System.out.println("negative");
} else {
System.out.println("not negative");
}
while (n < 200) {
n = n * 2;
}
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011