CSE143X Sample Midterm handout #8
Fall 2012
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
4 * (2 + 4) - 3 * 5 __________
54 % 10 + 8 * 3 % 9 __________
3 * 2 + 4 + "+" + 2 + 3 * 4 __________
2.3 * 3 + 19 / 5 / 2 + 6.0 / 5 __________
108 / 20 * 3 / 4 / 2.0 + 1.0 / 2 __________
2. Parameter Mystery, 12 points. Consider the following program.
public class Mystery {
public static void main(String[] args) {
String one = "student";
String two = "beer";
String three = "dorm";
int number = 12;
sentence(two, one, number);
sentence(three, two, 125);
sentence(three, one, 250);
sentence(one, two, 4);
}
public static void sentence(String one, String two, int number) {
System.out.println(two + "s in the " + one + " = " + number);
}
}
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) {
int c = 2;
if (a + c < b) {
c = c + 8;
} else {
b = b + 10;
}
if (a + c < b) {
c = c + 8;
} else {
b = b + 10;
}
System.out.println(b + " " + c);
}
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 int mystery(int x, int y) {
int z = 0;
while (y > 0) {
z = z + x;
x = x + y;
y--;
}
return z;
}
For each call below, indicate what value is returned:
Method Call Value Returned
mystery(6, 0) _______________
mystery(8, 1) _______________
mystery(3, 3) _______________
mystery(4, 2) _______________
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(Scanner console) {
int x = 0;
int y = 1;
int next = console.nextInt();
// Point A
while (next != 0) {
// Point B
y = y * next;
if (next < 0) {
x++;
// Point C
}
next = console.nextInt();
// Point D
}
// Point E
System.out.println(y + " " + x);
}
Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.
next < 0 next == 0 x > 0
+---------------------+---------------------+---------------------+
Point A | | | |
+---------------------+---------------------+---------------------+
Point B | | | |
+---------------------+---------------------+---------------------+
Point C | | | |
+---------------------+---------------------+---------------------+
Point D | | | |
+---------------------+---------------------+---------------------+
Point E | | | |
+---------------------+---------------------+---------------------+
6. Programming, 10 points. Write a static method called hasMidpoint that takes
three integers as parameters and that returns true if one of the numbers is
the midpoint of the other two and that returns false otherwise. A number is
considered the midpoint of two numbers if it appears halfway between them.
For example, 12 is the midpoint of 10 and 14. The numbers might be in any
order, so any one of the three might be the midpoint of the other two.
The table below shows sample calls and the value that should be returned.
Method Value Method Value
Call Returned Call Returned
--------------------------------- ---------------------------------
hasMidpoint(10, 12, 14) true hasMidpoint(14, 10, 12) true
hasMidpoint(12, 10, 14) true hasMidpoint(2, 10, 6) true
hasMidpoint(8, 8, 8) true hasMidpoint(25, 10, -5) true
hasMidpoint(2, 3, 4) true hasMidpoint(2, 3, 5) false
hasMidpoint(3, 1, 3) false hasMidpoint(2, 4, 5) false
hasMidpoint(21, 9, 58) false hasMidpoint(2, 8, 16) false
7. Programming, 10 points. Write a static method called flip that takes a
Random object and an integer n as parameters and that prints information
about a coin-flipping simulation. Your method should use the Random object
to produce a sequence of simulated coin flips, printing whether each flip
comes up "heads" or "tails." Each outcome should be equally likely. Your
method should stop flipping when you see n heads in a row (where n is the
second value passed as a parameter to the method).
For example, if we construct a Random object and make the following calls:
Random r = new Random();
flip(r, 2);
flip(r, 4);
We expect to get output like the following:
flips: H T T T T T T H H
flips: H H T H T H T H H H T T T T T H T H H H H
Notice that we abbreviate "heads" as "H" and "tails" as "T." You must
exactly reproduce the format of the log above, although the specific output
produced will vary on different executions because of the use of the Random
object to produce different sequences. You may assume that the integer
passed as a parameter to your method is greater than 0.
8. Arrays, 10 points. Write a static method called numUnique that takes a
sorted array of integers as a parameter and that returns the number of
unique values in the array. The array is guaranteed to be in sorted order,
which means that duplicates will be grouped together. For example, if a
variable called "list" stores the following values:
[5, 7, 7, 7, 8, 22, 22, 23, 31, 35, 35, 40, 40, 40, 41]
then the following call:
numUnique(list)
should return 9 because this list has 9 unique values (5, 7, 8, 22, 23, 31,
35, 40 and 41). It is possible that the list might not have any
duplicates. For example if list instead stored this sequence of values:
[1, 2, 11, 17, 19, 20, 23, 24, 25, 26, 31, 34, 37, 40, 41]
then a call on the method would return 15 because this list contains 15
different values.
If passed an empty list, your method should return 0. Remember that you can
assume that the values in the array appear in sorted (nondecreasing) order.
You are not allowed to use an auxiliary data structure such as a temporary
array or ArrayList to solve this problem and you are not allowed to call any
methods of the Arrays class or the Collections class.
9. Programming, 9 points. Write a method called samePattern that returns true
or false depending upon whether two strings have the same pattern of
characters. More precisely, two strings have the same pattern if they are
of the same length and if two characters in the first string are equal if
and only if the characters in the corresponding positions in the second
string are also equal. Below are some examples of patterns that are the
same and patterns that differ:
1st String 2nd String Same Pattern?
---------------------------------------------
asasasasas xyxyxyxyxy true
ascneencsa aeiouuoiea true
aaassscccn aaabbbcccd true
aaassscccn gggmmmfffo true
asasasasas xxxxxyyyyy false
ascneencsa aeiouaeiou false
aaassscccn xxxyyyzzzz false
aaasssiiii gggdddfffh false
Your method should take two parameters: the two strings to compare. To
receive full credit, you should solve this problem without the use of any
auxiliary data structures. You can receive up to 7 of the 9 points for a
solution that involves such structures.