CSE142A Midterm
Winter 2013
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
3 * 4 + 5 * 6 __________
23 % 5 - 17 % (16 % 10) __________
"1" + 2 + 3 * 4 + (5 + 6) __________
1.5 * 2 + 20 / 3 / 4.0 + 6 / 4 __________
345 / 10 / 3 + 10 / (5 / 2.0) __________
2. Parameter Mystery, 12 points. Consider the following program.
public class Mystery {
public static void main(String[] args) {
String foo = "buzz";
String sam = "sue";
String sue = "foo";
String bill = "hope";
String hope = "bill";
say(sam, sue, foo);
say(foo, "bill", sam);
say(bill, hope, sam);
say("sue", "hope", hope);
}
public static void say(String foo, String sam, String sue) {
System.out.println(sam + " wants " + sue + " to " + foo + ".");
}
}
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 int mystery(int z) {
int x = 1;
int y = 1;
while (z > 2) {
y = y + x;
x = y - x;
z--;
}
return y;
}
For each call below, indicate what value is returned:
Method Call Value Returned
mystery(3) _______________
mystery(4) _______________
mystery(5) _______________
mystery(6) _______________
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 int mystery(int x) {
int y = 1;
int z = 0;
// Point A
while (y <= x) {
// Point B
y = y * 10;
z++;
// Point C
}
// Point D
z--;
// Point E
return z;
}
Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.
y > x z < 0 z > 0
+---------------------+---------------------+---------------------+
Point A | | | |
+---------------------+---------------------+---------------------+
Point B | | | |
+---------------------+---------------------+---------------------+
Point C | | | |
+---------------------+---------------------+---------------------+
Point D | | | |
+---------------------+---------------------+---------------------+
Point E | | | |
+---------------------+---------------------+---------------------+
6. Programming, 15 points. Write a static method called printMultiples that
takes an integer n and an integer m as parameters and that prints a complete
line of output reporting the first m multiples of n. For example, the
following calls:
printMultiples(3, 5);
printMultiples(7, 3);
should produce this output:
The first 5 multiples of 3 are 3, 6, 9, 12, 15
The first 3 multiples of 7 are 7, 14, 21
Notice that the multiples are separated by commas. You are to exactly
reproduce this format. Also notice the order of the parameters: the first
parameter is the base number and the second parameter is the number of
multiples to generate.
You may assume that the number of multiples you will be asked to generate is
greater than or equal to one.
7. Programming, 15 points. Write a static method called digitRange that takes
an integer n as a parameter and that returns the greatest difference between
two digits of n. In particular, the method should report the largest
difference (x - y) where x and y are digits of n. For example, the call
digitRange(68437) should return 5 because the largest difference that can be
formed using digits from the number is (8 - 3). You may assume that the
number passed to the method is greater than or equal to 0. If the method is
passed a 1-digit number, it should return 0. Below are more examples of
calls and the values that should be returned.
Method Value Method Value
Call Returned Call Returned
--------------------------------- ---------------------------------
digitRange(0) 0 digitRange(888) 0
digitRange(5) 0 digitRange(1234) 3
digitRange(26) 4 digitRange(24680) 8
digitRange(42) 2 digitRange(857492) 7
digitRange(725) 5 digitRange(3876254) 6
You are not allowed to use a String to solve this problem.
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
+---------+
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