CSE142 Midterm
Winter 2010
Name of Student_______________________________________________________________
Section (e.g., AA)___________________ TA_____________________________________
This is an open-book/open-note exam. Space is provided for your answers. Use
the backs of pages if necessary. The 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 _____
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
5 * 6 - (4 + 3) * 2 - 2 * 3 ________________
208 / 20 / 4 + 12 / 10.0 + 0.4 * 2 ________________
8 - 2 + "8 - 2" + (8 - 2) + 8 ________________
4 * 5 % 6 + 297 % 10 + 4 % 8 ________________
13 / 2 * 3.0 + 5.5 * 3 / 2 ________________
2. Parameter Mystery, 12 points. Consider the following program.
public class Mystery {
public static void main(String[] args) {
String he = "we";
String says = "can";
String yes = "he";
String we = "yes";
slogan(we, yes, says);
slogan(says, "Barry", "maybe");
slogan(says, he, yes);
slogan("can't", "Sarah", "no");
}
public static void slogan(String can, String he, String yes) {
System.out.println(he + " says " + yes + " we " + can);
}
}
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) {
if (a < b) {
a++;
}
if (a < b) {
a++;
} else {
b++;
}
if (a >= b) {
b = b - 5;
}
System.out.println(a + " " + b);
}
For each call below, indicate what output is produced.
Method Call Output Produced
ifElseMystery(1, 8); _______________
ifElseMystery(3, 5); _______________
ifElseMystery(4, 5); _______________
ifElseMystery(8, 6); _______________
4. While Loop Simulation, 12 points. Consider the following method:
public static void mystery(int n) {
int x = 1;
int y = 1;
while (n > y) {
x++;
y = 10 * y + x;
}
System.out.println(x + " " + y);
}
For each call below, indicate what output is produced.
Method Call Output Produced
mystery(0); _______________
mystery(7); _______________
mystery(32); _______________
mystery(256); _______________
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(int x, int y) {
int z = 0;
// Point A
while (x < y) {
// Point B
z++;
if (z % 2 == 0) {
x *= 2;
// Point C
} else {
y--;
// Point D
}
}
// Point E
System.out.println(z);
}
Fill in the table below with the words ALWAYS, NEVER, or SOMETIMES (you may
abbreviate them as A, N, or S).
x < y z == 0 z % 2 == 0
+---------------------+---------------------+---------------------+
Point A | | | |
+---------------------+---------------------+---------------------+
Point B | | | |
+---------------------+---------------------+---------------------+
Point C | | | |
+---------------------+---------------------+---------------------+
Point D | | | |
+---------------------+---------------------+---------------------+
Point E | | | |
+---------------------+---------------------+---------------------+
6. Programming, 15 points. Write a static method called hasOddEven that takes
three integers as parameters and that returns true if there is at least one
odd and at least one even among the three numbers and that returns false
otherwise. Below are some sample calls and the appropriate value to return.
Method Value Method Value
Call Returned Call Returned
------------------------------- -------------------------------
hasOddEven(2, 4, 6) false hasOddEven(14, 7, 5) true
hasOddEven(2, 3, 4) true hasOddEven(5, 4, 2) true
hasOddEven(12, 4, 17) true hasOddEven(13, 20, 91) true
hasOddEven(5, 17, 4) true hasOddEven(7, 19, 5) false
You may assume that all three integers passed to your method are greater
than or equal to 0.
7. Programming, 15 points. Write a static method called findRange that
takes a console Scanner as a parameter and that prompts the user for a
sequence of integers, reporting the greatest difference among the integers.
The method should prompt for integers until the user enters a 0 and it
should then report the greatest difference between a pair of numbers from
the sequence (i.e., the largest value for x - y where x and y are numbers
from the sequence). For example, the following call:
Scanner console = new Scanner(System.in);
findRange(console);
would generate an interaction like this:
number (0 to quit)? 45
number (0 to quit)? 3
number (0 to quit)? 8
number (0 to quit)? 54
number (0 to quit)? 0
range = 51
You must exactly reproduce the format of this sample execution. The method
reports 51 as the range because the greatest difference between two of these
numbers is obtained when you take 54 minus 3. Notice that the sentinel
value of 0 is not considered part of the sequence. The sequence might
include negative values, as in the following interaction:
number (0 to quit)? 8
number (0 to quit)? -17
number (0 to quit)? 5
number (0 to quit)? 0
range = 25
The method reports a range of 25 because the greatest difference is obtained
by taking 8 minus -17. You may assume that the user types at least two
nonzero values. The range would be 0 if all numbers in the sequence are
the same.
8. Programming, 9 points. Write a static method called undouble that takes a
string as a parameter and that returns a new string obtained by replacing
every pair of repeated adjacent letters with one of that letter. For
example, the String "bookkeeper" has three repeated adjacent letters ("oo",
"kk", and "ee"), so undouble("bookkeeper") should return the string
"bokeper". Below are more sample calls:
Method Value Method Value
Call Returned Call Returned
--------------------------------- ---------------------------------
undouble("odegaard") "odegard" undouble("oops") "ops"
undouble("baz") "baz" undouble("foobar") "fobar"
undouble("mississippi") "misisipi" undouble("apple") "aple"
undouble("carry") "cary" undouble("berry") "bery"
undouble("juggle") "jugle" undouble("theses") "theses"
undouble("little") "litle" undouble("") ""
You may assume that the string is composed entirely of lowercase letters, as
in the examples above, and that no letter appears more than two times in a
row. But notice that the method might be passed an empty string, in which
case it returns an empty string.
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
+---------+
Stuart Reges
Last modified: Fri Sep 16 15:36:12 PDT 2011