CSE142 Midterm
Winter 2011
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. You are allowed to have one
hand-written index card up to 5" x 8". 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
13 + 2 * 5 / 3 ________________
2.5 * 2 * 5 / 10 + 1.5 ________________
85 % 10 + 4 % 10 - 17 % 3 ________________
2 + 3 + "." + (3 + 4) + 2 * 3 ________________
482 / 10 / 5 / 2.0 * 2 + 14 / 5 ________________
2. Parameter Mystery, 12 points. Consider the following program.
public class Mystery {
public static void main(String[] args) {
String john = "skip";
String mary = "george";
String george = "mary";
String fun = "drive";
statement(mary, john, fun);
statement(fun, "george", "work");
statement(george, mary, john);
statement(george, "john", "dance");
}
public static void statement(String mary, String john, String fun) {
System.out.println(john + " likes to " + fun + " with " + mary);
}
}
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) {
b--;
} else if (a < b) {
a++;
} else {
b = b + 5;
}
if (a == b) {
a = a + 2;
}
System.out.println(a + " " + b);
}
For each call below, indicate what output is produced.
Method Call Output Produced
ifElseMystery(14, 14); _______________
ifElseMystery(4, 5); _______________
ifElseMystery(10, 5); _______________
ifElseMystery(2, 8); _______________
4. While Loop Simulation, 12 points. Consider the following method:
public static void mystery(int y) {
int x = 0;
int z = 0;
while (y > 0) {
x++;
z = z + y % 10;
y = y / 10;
}
System.out.println(x + " " + z);
}
For each call below, indicate what output is produced.
Method Call Output Produced
mystery(8); _______________
mystery(32); _______________
mystery(184); _______________
mystery(8239); _______________
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(Scanner console) {
int prev = 0;
int count = 0;
int next = console.nextInt();
// Point A
while (next != 0) {
// Point B
if (next == prev) {
// Point C
count++;
}
prev = next;
next = console.nextInt();
// Point D
}
// Point E
return count;
}
Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.
next == 0 prev == 0 next == prev
+---------------------+---------------------+---------------------+
Point A | | | |
+---------------------+---------------------+---------------------+
Point B | | | |
+---------------------+---------------------+---------------------+
Point C | | | |
+---------------------+---------------------+---------------------+
Point D | | | |
+---------------------+---------------------+---------------------+
Point E | | | |
+---------------------+---------------------+---------------------+
6. Programming, 15 points. Write a method called numUnique that takes three
integers as parameters and that returns the number of unique integers among
the three. For example, if the following call is made:
numUnique(18, 3, 4)
the method should return 3 because the parameters represent 3 different
numbers (the values 18, 3 and 4). By contrast, the following call:
numUnique(6, 6, 6)
would return 1 because there is only 1 unique number among the three
parameters (the value 6). The values passed to your method might appear in
any order whatsoever, so you cannot assume that duplicate values would
appear together. For example, if the following call is made:
numUnique(7, 31, 7)
the method should return 2 because there are 2 unique numbers among the
parameters (the values 7 and 31).
7. Programming, 15 points. Write a method called printNumbers that takes a
Random object as a parameter and that prints a list of randomly generated
numbers ranging from 1 to 50 inclusive (each number being equally likely).
The list should be surrounded by square brackets and the numbers should be
separated by commas. The method should randomly generate numbers until it
generates one that ends in 5. A typical call would look like this:
Random r = new Random();
printNumbers(r);
The method might fairly quickly generate a number ending in 5:
[43, 34, 27, 2, 2, 25]
Or it might take a while to get to a number ending in 5:
[23, 8, 13, 1, 37, 37, 9, 34, 23, 34, 4, 9, 16, 44, 49, 43, 49, 3, 45]
It is also possible that it will immediately generate a number ending in 5:
[35]
You must exactly reproduce the format of these examples.
8. Programming, 9 points. Write a method called numWords that takes a String
as a parameter and that returns the number of words in the String. By
definition, words are separated by one or more spaces. The table below
shows several sample calls and the value that should be returned.
Method Call Value Returned
---------------------------------------------------- --------------
numWords("how many words here?") 4
numWords("to be or not to be, that is the question") 10
numWords(" how about merry-go-round ") 3
numWords(" !&$%--$$!!*() foo_bar_baz ") 2
numWords("x") 1
numWords(" ") 0
numWords("") 0
Notice that words can contain punctuation marks. Any non-empty sequence of
non-space characters can be a word. Also notice that there might be spaces
at the beginning or end of the String.
You may not construct any other objects to solve this problem (e.g., you
can't use a Scanner or tokenizer). You may assume that the String has no
other whitespace characters such as tabs or newline characters. Your method
can pay attention just to spaces to decide how many words there are.
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
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 15:36:27 PDT 2011