CSE142 Midterm
Spring 2016
Name of Student_______________________________________________________________
Section (e.g., AA)___________________ TA_____________________________________
This exam is divided into eight 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 _____
----------------------------
Total 100 _____
This is a closed-book/closed-note exam. Space is provided for your answers.
You can also request scratch paper from a TA. You are not allowed to access
any of your own papers during the exam.
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 and smart watches. 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 a TA 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 foo = "buzz";
String sam = "sue";
String sue = "foo";
String bill = "hope";
String hope = "bill";
say(foo, "bill", sam);
say(hope, bill, sue);
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 % 2 == 0) {
a++;
b--;
} else if (a % 2 == 1) {
b = 0;
}
if (b == 0 && b != a) {
a = a + 2;
}
System.out.println(a + " " + b);
}
For each call below, indicate what output is produced.
Method Call Output Produced
ifElseMystery(5, 20); _______________
ifElseMystery(42, 42); _______________
ifElseMystery(6, 1); _______________
ifElseMystery(2, 0); _______________
ifElseMystery(7, 10); _______________
ifElseMystery(4, 4); _______________
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(int n) {
int x = 0;
// Point A
while (n != 0) {
// Point B
if (n % 2 == 1) {
x++;
// Point C
}
n = n / 10;
// Point D
}
// Point E
return x;
}
Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.
n == 0 n % 2 == 1 x == 0
+---------------------+---------------------+---------------------+
Point A | | | |
+---------------------+---------------------+---------------------+
Point B | | | |
+---------------------+---------------------+---------------------+
Point C | | | |
+---------------------+---------------------+---------------------+
Point D | | | |
+---------------------+---------------------+---------------------+
Point E | | | |
+---------------------+---------------------+---------------------+
6. Programming, 15 points. Write a static method called testLuck that
takes a console Scanner and an integer n as parameters and that tests how
lucky a user is at guessing a one-digit odd number n different times,
reporting the results. There are five different one-digit odd numbers (1,
3, 5, 7, and 9), so the user would be expected to get one-fifth of these
correct with random guessing. Below are two sample calls on the method:
testLuck(console, 3);
testLuck(console, 5);
Below are sample logs of what might happen (in the left-hand log the user
enters the guesses 3, 7, and 5, and in the right-hand log the user enters
the guesses 1, 3, 5, 7, 9):
guess 3 one-digit odd numbers guess 5 one-digit odd numbers
guess #1? 3 guess #1? 1
incorrect...answer was 1 incorrect...answer was 3
guess #2? 7 guess #2? 3
correct! correct!
guess #3? 5 guess #3? 5
incorrect...answer was 3 correct!
expected correct = 0.6 guess #4? 7
you got 1 correct incorrect...answer was 5
guess #5? 9
incorrect...answer was 1
expected correct = 1.0
you got 2 correct
You are to exactly reproduce the format of these logs. Your method should
construct a Random object to allow it to pick different numbers each time.
7. Programming, 15 points. Write a static method called printSequenceTo that
takes a target value as a parameter and that prints terms from a particular
numerical sequence until they add up to a value greater than or equal to the
target and that returns the number of terms that were included. For
example, if the following calls are made:
int n1 = printSequenceTo(3.0);
int n2 = printSequenceTo(5.5);
The following output should be produced:
1/2 + 2/3 + 3/4 + 4/5 + 5/6 = 3.5500000000000003
1/2 + 2/3 + 3/4 + 4/5 + 5/6 + 6/7 + 7/8 + 8/9 = 6.171031746031746
The variable n1 is set to 5 because it took 5 terms from the sequence to get
a sum that is at least 3.0. The variable n2 would be set to 8 because it
took 8 terms to get a sum that is at least 5.5. You are to exactly
reproduce the format of this output. You may assume that the target is
greater than 0. Notice that the sum is not rounded.
8. Programming, 9 points. Write a method called isConsecutive that takes an
integer n as a parameter and that returns true if the digits of n are
consecutive. Recall that consecutive digits come one after another (1
followed by 2 followed by 3, and so on). Your method should return true for
a one-digit number.
The table below shows sample calls and the value that should be returned.
Method Value Method Value
Call Returned Call Returned
--------------------------------- ---------------------------------
isConsecutive(0) true isConsecutive(8) true
isConsecutive(11) false isConsecutive(12) true
isConsecutive(21) false isConsecutive(22) false
isConsecutive(23) true isConsecutive(1235) false
isConsecutive(1234) true isConsecutive(89) true
isConsecutive(890) false isConsecutive(987) false
You may not use a String or other object to solve this problem. You may
assume that the number passed to your method is not negative, but it might
be 0.
Stuart Reges
Last modified: Fri Sep 16 16:16:07 PDT 2011