CSE143X Midterm
Fall 2012
Name of Student_______________________________________________________________
Section (e.g., AA)___________________ TA_____________________________________
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 10 _____
7 Programming 10 _____
8 Arrays 10 _____
9 Programming 9 _____
----------------------------
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 7 of
the textbook. You may assume that appropriate import declarations have been
included to provide access to standard classes, but you are not allowed to use
classes or methods that aren't listed on the cheat sheet.
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.
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 * (5 - 2) - 3 - 2 * 2 __________
4 * 7 % 8 + 132 % 10 + 3 % 4 __________
27 / 5 / 2 + 3.4 * 2 - 1.1 * 2 __________
9 + 9 + "9 + 9" + 9 + 9 __________
19 / 2 / 2.0 + 2.5 * 6 / 2 + 0.5 * 4 __________
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(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 = 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(2); _______________
mystery(4); _______________
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, 10 points. Write a static method called quadrant that takes as
parameters a pair of double values representing an (x, y) point and that
returns the quadrant number for that point. Recall that quadrants are
numbered as integers from 1 to 4 with the upper-right quadrant numbered 1
and the subsequent quadrants numbered in a counter-clockwise fashion:
^ y-axis
|
|
|
Quadrant 2 | Quadrant 1
|
<--------------------+--------------------> x-axis
|
Quadrant 3 | Quadrant 4
|
|
|
V
Notice that the quadrant is determined by whether the x and y coordinates
are positive or negative numbers. If a point falls on the x-axis or the
y-axis, then the method should return 0. Below are sample calls on the
method.
Method Value Method Value
Call Returned Call Returned
------------------------------- -------------------------------
quadrant(12.4, 17.8) 1 quadrant(0.0, 0.0) 0
quadrant(-2.3, 3.5) 2 quadrant(12.5, 0.0) 0
quadrant(-15.2, -3.1) 3 quadrant(0.0, 2.3) 0
quadrant(4.5, -4.5) 4
7. Programming, 10 points. Write a static method called minHailstoneValue that
takes integers n and m as parameters and that returns the minimum value in a
hailstone sequence of length m that begins with n. In a hailstone sequence,
each value x is followed either by:
(3x + 1) if x is odd
(x/2) if x is even
For example, if we start with 7 and we construct a sequence of length
10, we get:
7, 22, 11, 34, 17, 52, 26, 13, 40, 20
In this case, the minimum value in the sequence is 7 (the number we started
with). Therefore, the following call:
minHailstoneValue(7, 10)
should return 7. If we instead construct a sequence of length 20, we get:
7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1
In this case, the minimum value in the sequence is 1. Therefore, the call:
minHailstoneValue(7, 20)
should return 1. You may assume that both integers passed to your method
are greater than 0.
8. Arrays, 10 points. Write a static method called isSorted that takes an
array of doubles as an argument and that returns true if the list is in
sorted (nondecreasing) order and that returns false otherwise. For example,
if variables called list1 and list2 store the following values:
list1: [16.1, 12.3, 22.2, 14.4]
list2: [1.5, 4.3, 4.3, 7.0, 19.5, 25.1, 46.2]
then the call isSorted(list1) should return false while the call
isSorted(list2) should return true. By definition, an array with fewer than
two elements is considered to be sorted.
9. Programming, 9 points. Write a static method called printCompact that takes
a string as an argument and that prints a complete line of output with the
text of the string in compact form. The idea is that we might have a string
that has a lot of extra spaces in it separating words. For example, if you
make the call:
printCompact(" this string has lots of spaces ");
the method should produce this line of output:
this string has lots of spaces
This line of output has just 5 spaces separating the six words (no spaces at
the beginning or the end of the line).
If the string passed to your method is empty or contains just spaces, your
method should produce a blank line as output. You may assume that the
string does not contain tab characters or other whitespace characters that
behave like spaces.
You are not allowed to use any auxiliary data structures (no String, array,
Scanner, etc) and your solution must run in O(n) time where n is the length
of the string. You can receive partial credit if your solution requires
such a structure or takes longer to execute.
CSE143X Midterm Cheat Sheet
Primitive types: int, double, char, boolean
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;
}
Array examples:
for (int i = 0; i < list.length; i++) {
System.out.print(list[i] + " ");
}
for (int n : list) {
System.out.print(n + " ");
}
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011