Good luck!
|
Problem |
Description |
Earned |
Max |
|
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 |
Total Points |
|
100 |
1. Expressions (10 points)
For each expression in the left column, indicate its value in the right column. List a value of appropriate type and capitalization, e.g., 7 for an int, 7.0 for a double, "hello" for a String, true or false for a boolean.
|
Expression |
Value |
|
2.5 * 2 * 5 / 10 + 1.5 85 % 10 + 4 % 10 - 17 % 3 (2 + 2 < 3 || 2 * 5
/ 3 == 3) == false 2 + 3 + "."
+ (3 + 4) + 2 * 3 482 / 10 / 5 / 2.0 * 2
+ 14 / 5 |
_____________________________ _____________________________ _____________________________ _____________________________ _____________________________ |
2. Parameter Mystery (12 points)
At the bottom of the page, write the output produced by the following program, as it would appear on the console.
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(sam,
sue, foo);
say(foo,
"bill", sam);
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 + ".");
}
}
3. If/Else Simulation (12 points)
For each call below to the following method, write the output that is produced, as it would appear on the console:
public static void ifElseMystery(int a, int b) {
if (a > b) {
a = a - 3;
b++;
} else {
b = b - 4;
a--;
}
if (a < b) {
a = a + 10;
} else if (a
> b) {
b = b + 20;
}
System.out.println(a
+ " " + b);
}
|
Method Call |
Output |
|
ifElseMystery(5, 2); ifElseMystery(8, 3); ifElseMystery(6, 2); ifElseMystery(2, 8); ifElseMystery(6, 7); ifElseMystery(4, 7); |
________________________________________ ________________________________________ ________________________________________ ________________________________________ ________________________________________ ________________________________________ |
4. While Loop Simulation (12 points)
For each call below to the following method, write the output that is produced, as it would appear on the console:
public
static void whileMystery(int y) {
int x = 0;
int z = 0;
while (y > 0) {
x++;
z = z + y % 10;
y = y / 10;
}
System.out.println(x + " " + z);
}
|
Method Call |
Output |
|
whileMystery(8); whileMystery(8239); |
____________________________________________ ____________________________________________ ____________________________________________ ____________________________________________ |
5. Assertions (15 points)
For each of the five
points labeled by comments, identify each of the assertions in the table below
as either being always true, never true, or sometimes true / sometimes false.
(You may abbreviate them as A, N, or S.)
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;
}
|
|
next == 0 |
prev == 0 |
next == prev |
|
Point A |
|
|
|
|
Point B |
|
|
|
|
Point C |
|
|
|
|
Point D |
|
|
|
|
Point E |
|
|
|
6. Programming (15 points)
Write a static method called tallyVotes that takes a console scanner as a parameter and that prompts the user for a series of votes, reporting and returning the percentage of yes votes. The user is prompted for a series of responses that are either "y" to indicate yes, "n" to indicate no, or "q" to indicate that the user wants to quit because there are no more votes to tally. You can assume that the user will always type one of these three responses as a lowercase letter.
For example, below is a sample call on the method:
Scanner console = new
Scanner(System.in);
tallyVotes(console);
This call would produce an interaction like the following (user input is in bold):
vote? y
vote? y
vote? n
vote? y
vote? y
vote? y
vote? q
total votes = 6
result =
83.33333333333333%
The value 83.33333333333333 would also
be returned. Notice that the user is prompted for votes until they quit and
then the method produces a line of output showing the total number of votes and
a line of output showing the percent of yes votes. You do not need to round the
percentage. You are to exactly reproduce the format of this log. You may assume
that the user enters at least one vote before quitting.
7. Programming (15 points)
Write a static method called simulateTeenagers that takes an integer n and a Random object as parameters and that generates a group of n different teenage ages, reporting the average age and the minimum age. Recall that teenagers range in age from 13 to 19 inclusive. Each age should be equally likely to be generated by your method.
For example, if you make the following calls:
Random r = new Random();
simulateTeenagers(6, r);
System.out.println();
simulateTeenagers(10, r);
you would expect to get output like the following:
ages: 18, 18, 13, 15, 18,
13
average =
15.833333333333334
minimum = 13
ages: 17, 15, 14, 15, 18,
16, 17, 18, 19, 18
average = 16.7
minimum = 14
Notice that the method produces three lines of output. The first line contains the list of n ages generated by the method. The second and third lines report the average age and the minimum age. You do not need to round the average printed. You are to exactly reproduce the format of these logs. You may assume that the integer passed to your method is greater than 0.
8. Programming (9 points)
Write a static method called digitsInARow that takes an integer n as a parameter and that returns the highest number of digits that appear in a row in the base-10 representation of n. For many numbers the answer will be 1 because they don't have adjacent digits that match. But for a number like 3555585, the answer is 4 because there are four occurrences of the digit 5 that appear in a row. Below are sample calls on the method.
|
Method Call |
Value Returned |
|
digitsInARow(0) |
1 |
|
digitsInARow(8823) |
2 |
|
digitsInARow(18) |
1 |
|
digitsInARow(777) |
3 |
|
digitsInARow(394) |
1 |
|
digitsInARow(82888) |
3 |
|
digitsInARow(99) |
2 |
|
digitsInARow(7111171) |
4 |
|
digitsInARow(8229) |
2 |
|
digitsInARow(233333888) |
5 |
You may assume that the integer passed as a parameter to your method is greater than or equal to 0. You may not use Strings to solve this problem.