CSE142B 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
16 / 3 + 3.2 * 2 __________
8 / 7 + "5 / 4" + 8 / 3 __________
88 % 10 % 3 * 16 / 10 __________
29 / 3 / 2 / 4.0 + 3.6 * 2 __________
1.4 + (3 + 2 * 6) / (8 - 14 / 3) * 2.2 __________
2. Parameter Mystery, 12 points. Consider the following program.
public class Mystery {
public static void main(String[] args) {
String hear = "bad";
String song = "good";
String good = "hear";
String walk = "talk";
String talk = "feel";
String feel = "walk";
claim(feel, song, feel);
claim(good, hear, song);
claim(talk, "song", feel);
claim(talk, "bad", walk);
}
public static void claim(String hear, String good, String song) {
System.out.println("to " + hear + " the " + song + " is " + good);
}
}
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(4, 15); _______________
ifElseMystery(7, 17); _______________
ifElseMystery(12, 5); _______________
ifElseMystery(16, 8); _______________
4. While Loop Simulation, 12 points. Consider the following method:
public static void mystery(int n) {
int x = 1;
int y = 2;
while (y < n) {
if (n % y == 0) {
n = n / y;
x++;
} else {
y++;
}
}
System.out.println(x + " " + n);
}
For each call below, indicate what output is produced:
Method Call Output Produced
mystery(4); _______________
mystery(5); _______________
mystery(24); _______________
mystery(28); _______________
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 (x > y) {
// Point C
x = x / 10;
} else {
// Point D
y = y / 10;
}
}
// Point E
System.out.println(x + " " + y + " " + z);
}
Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.
x > y z == 0 x == y
+---------------------+---------------------+---------------------+
Point A | | | |
+---------------------+---------------------+---------------------+
Point B | | | |
+---------------------+---------------------+---------------------+
Point C | | | |
+---------------------+---------------------+---------------------+
Point D | | | |
+---------------------+---------------------+---------------------+
Point E | | | |
+---------------------+---------------------+---------------------+
6. Programming, 15 points. Write a static method called printTwoDigit that
takes a Random object and an integer n as parameters and that prints a
series of n randomly generated two-digit numbers. The method should use the
Random object to select numbers in the range of 10 to 99 inclusive where
each number is equally likely to be chosen. The method should indicate
whether the number 42 was selected. For example, given the following code:
Random r = new Random();
printTwoDigit(r, 3);
You would expect output like the following:
next = 52
next = 10
next = 86
no 42
As the final line of output above indicates, that particular sequence did
not include the number 42. Suppose that we then call the method as follows:
printTwoDigit(r, 5);
We might get output like the following where 42 is included in the sequence:
next = 83
next = 42
next = 22
next = 36
next = 73
We saw a 42
You may assume the integer value passed to your method is greater than or
equal to 0. You are to exactly reproduce the format of these logs.
7. Programming, 15 points. Write a static method called hasOddEvenDigit that
takes an integer n as a parameter and that returns true if n has both an odd
digit and an even digit and that returns false otherwise. For example, the
call hasOddEvenDigit(68437) should return true because the number has both
odd digits (3 and 7) and even digits (6, 8, and 4). Below are more examples
of calls and the values that should be returned.
Method Value Method Value
Call Returned Call Returned
--------------------------------- ---------------------------------
hasOddEvenDigit(0) false hasOddEvenDigit(808) false
hasOddEvenDigit(5) false hasOddEvenDigit(319751) false
hasOddEvenDigit(10) true hasOddEvenDigit(8226408) false
hasOddEvenDigit(34) true hasOddEvenDigit(135679) true
hasOddEvenDigit(55) false hasOddEvenDigit(72468) true
You are not allowed to use a String to solve this problem.
8. Programming, 9 points. Write a static method called printStripped that
takes a string as a parameter and that prints a complete line of output with
any comments stripped from the string. Comments are defined to be
characters enclosed in the characters "<" and ">". More precisely, text is
"normal" until you encounter a "<" character. From that point on the text
is considered a comment until you encounter a ">" character, at which point
you return to normal text. This definition allows for "<" inside a comment
and ">" outside a comment. You may assume that there are no unclosed
comments in the string.
For example, the following sequence of calls:
printStripped("this is plain text");
printStripped("this has a normal comment to be removed");
printStripped("this has multiple less-than in a comment <<<<");
printStripped("this > has greater-than outside a comment >>");
printStripped("this has multiple comments<>.");
should produce the following output:
this is plain text
this has a normal comment to be removed
this has multiple less-than in a comment
this > has greater-than outside a comment >>
this has multiple comments.
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