Download My Midterm Scanned

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

List of Static supporting characters wikipedia , lookup

Transcript
CSE142 Midterm
Autumn 2015
This exam is divided into eight questions with the following points:
#
Problem Area
1
Points
Score
Expressions
10
og
2
Parameter Mystery
12
3
If/Else Simulation
12
4
While Loop Simulation
12
5
Assertions
15
\1^
\1^
i-u
II
6
Programming
15
13
7
Programming
15
8
Programming
9
Total
H
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 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 throuqh 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 consta
appropriate type (e.g., 7.0 rather than 7 for a double. Strings in quot
Expression
13 + 2 * 5 / 3
16
3v
a.5
4,0
2.5*2*5/ 10+1.5
85--%^
+
4-%-±0
..2--K-3 +
-
Value
i(
17-^-^
-
+ (^—K-4.) +-2-^^
t(
s,76
[
482-7^<Lj3 / 5/ 2.0*2 + 14/5
(.0
<1
^ 0
-j
2. Parameter Mystery, 12 points.
^
Consider the following program.
public class Mystery {
public static void main(Strihg[] args) {
String she = "it";
String it = "her";
String her = "you";
String yoo_5_|liJa^;
saying(yoh^ i^, you);
saying(it, her, she);
saying(she, "you", her);
saying(it, "him", "fred");
public static void saying(String it. String her. String she) {
System.out.println(she + " can't take " + it + " with " + her);
List below the output produced by this program.
hike
<yf\ -V
'^(Xj C<5ti'4
h \A!\^h ycU
{^(5^ co/iM ^(\ki \f\^
1^1 n)
Simulation, 12 points.
Consider the following method
public static void ifElseMystery(int a, int b) {
if (a < b) {
a = a + 2;
tj
b,= b - 3;
U.
} else if (a == b) {
a = a * 2;
IO
b = b + 5;
IO
} else {
a = a - 3;
5 >3
}
if (a == b) {
b = b + 10;
•6
5
5
System.out.println(a + " " + b);
15
}
For each call below, indicate what output is produced.
Output Produced
Method Call
g
ifElseMystery(2, 5)
ifElseMystery(5, 5)
ifElseMystery(6, 3)
10
20
3
______
_____3_______
ifElseMystery(4, 4)
5
ifElseMystery(3, 8)
G
5
ifElseMystery(9, 5)
4. While Loop Simulation, 12 points.
Consider the following method:
public static void mystery(int x) {
int y = 1;
int z =
while (x > z) {
if (x % z == 0) {
X = X- / z;
y++;
} else {
3
Z++;
}
}
System.out.println(y + "
1
a
-z
3
+ z);
}
For each call below, indicate what output is produced:
Method Call
mystery(2);
Output Produced
a
-2.
mystery(5);
mysteryj^; ^
mystery(12);
g
a
Y
3
5. Assertions, 15 points.
You will identify various assertions as bein
always true, never true or sometimes true/sometimes false at various P
in program execution. The comments in the method below indicate the po
of interest.
public static void mystery(Scanner console) {
int y = 42;
int z = 0;
// Point A
while (y != 0) {
^^
^
// Point B
y = console.nextint0;
if (y % 2 == 0) {
Z++;
// Point C
lAh
I
// Point D
, ro
/^ '0
wc /)d<^
Od 0^^
o
0
AA
y--;
}
// Point E
System.out .printlnC'z =
+ z);
Fill in the table below with the words ALWAYS, NEVER or SOMETIMES.
Point A I
K/
■
z == 0
y % 2 == 0
y == 0
+
I
\
f\
+
\
I
Point B I
+
Point D I
+
'
I
.^A .......i...
1
I
.. y....................... I
....rSL....... i- --S.....................
....i-
+
_________ +
+
Point E I
3
.................... ...
Point C I
+
I
+
+
.........
...........
+
i
+
L
u 0 ' 'j 3^
^
points. Write a. static method called spinWheel that takes a
id an'integer n as parameters and that simulates the spinning
the number 20 comes up n times in a row. On the wheel are
30, 40, 50, and 60 and each number should be equally likely
the wheel is spun. Your method should report the individual
5 indicating how many times it takes to get n occurrences of
Dr example, below are two sample calls:
= new Random 0;
Mr, 2);
Mr, 3);
should produce two lines of output like this:
40, 50, 20, 50, 50, 40, 20, 30, 40, 50, 20, 20
after 13 spins
should produce two lines of output like this:
50, 50, 20, 40, 20, 40, 20, 20, 20
I after 10 spins
spin values ape separated by commas and that the method
las seen n occurrences of the value 20 in a row. You are to
ice the format of these logs. You may assume that the value n
method is greater than or equal to 1.
void
p /fii O' 5 piW;; > rN
J
0y
a
non
3
3
P<(\^j C6^K\\ ++;
, (c
J ^
\
H
\
J
4-2)^/0
6 ■
2)$
f
i-f- Covm
Covn-^
0\
Sy5|(fnA,(vi"'Pr''l4lri
' /
V
-t
ng, 15 points. Write a static method called spinWheel that takes a
,m object and an integer n as parameters and that simulates the spinning
f' a wheel until the number 20 comes up n times in a row. On the wheel are
the numbers 20, 30, 40, 50, and 60 and each number should be equally likely
to come up when the wheel is spun. Your method should report the individual
spins as well as indicating how many times it takes to get n occurrences of
20 in a row. For example, below are two sample calls:
c *■
'— S'
^ ^
Random r = new RandomO;
spinWheel(r, 2);
spinWheel(r, 3);
The first call should produce two lines of output like this:
spins: 40, 40, 50, 20, 50, 50, 40, 20, 30, 40, 50, 20, 20
2 in a row after 13 spins
The second call should produce two lines of output like this:
spins: 50, 50, 50, 20, 40, 20, 40, 20, 20, 20
3 in a row after 10 spins
Notice that the spin values are separated by commas and that the method
stops when it has seen n occurrences of the value 20 in a row. You are to
exactly reproduce the format of these logs. You may assume that the value n
passed to your method is greater than or equal to 1.
P P
1a I pri^ViWS^j/
I
•
J
a
3
3
T "5 W '^S
i/^hi
(c s>.'^A+1^.'
H
)
¥2))elD
( f
)^
2
13
Covm
N
<r
S
'C|0VV'''+
^
— O'
__
u ^
r^p'iWsFt;
f nVWAC);^'^' Svsfew.(vTiPr3Hn
gpiVs +
sj iAv-S 0/ ^
[r(AA\
II
Programming, 15 points. Write a static method called balanceCheckbd
takes a console Scanner as a parameter and that prompts a user for
information about transactions for a bank account, reporting the new b
after each transaction and the minimum balance at the end and returning
whether or not the account was ever overdrawn (true if it was, false if i
was not). The user is prompted for an initial balance, the number of
transactions to process, and the individual transaction amounts. Deposits
to the account are entered as positive numbers and checks and ATM
withdrawals are entered as negative numbers. A new balance is reported
after each transaction. For example, the method would be called as follows:
Scanner console = new Scanner(System.in);
balanceCheckbook(console);
Below are two sample logs of execution that might be produced:
initial balance? 48.50
how many transactions? 4
1/4 amount? -20.00
new balance = $28.5
2/4 amount? -5.75
new balance = $22.75
3/4 amount? 138.20
new balance = $160.95
4/4 amount? -20.00
new balance = $140.95
minimum balance = $22.75
initial balance? 39.75
how many transactions? 5
1/5 amount? -18.50
new balance = $21.25
2/5 amount? -7.20
new balance = $14.05
3/5 amount? -23.10
new balance = $-9.05
4/5 amount? 50.00
new balance = $40.95
5/5 amount? -8.45
new balance = $32.5
minimum balance = $-9.05
In the log to the left, the user enters 4 different transactions and the
minimum balance is not negative, so the method would return false to
indicate that the account was never overdrawn. In the log to the right, the
user enters 5 transactions and the minimum balance is negative, so the
method would return true to indicate that the account was overdrawn. You
are to exactly reproduce the format of these logs. You may assume that the
number of transactions entered by the user is at least 1.
Space is provided on the opposite page for your answer.
y
r
.. provide your answer to balanceCheckbook below.
ySI<rn.aAf)i'f)^'ir),-l,'^( Marcel j>\
VJ+ np^-
Aojiole ohn - ^ I
fisi' Cm
I ^ JL' ' i
''-y
tT'-
-^/ ' <-n
■
■
JU
M
_
’^(mV
t'l
;,s‘“-“'«
fr\M - d
i
''i
(
{!-r\
t
V
I
n
or-
8, Programming, 9 points. Write a static method called sameDashes th
two strings as parameters and that returns whether or not they have d
in the same places (returning true if they do and returning false
otherwise). For example, below are four pairs of strings of equal lengt
that have the same pattern of dashes. Notice that the last pair has no
dashes at all.
Stringl:
stringZ:
"hi--there-you."
"12--(134)-7539"
"-15-389"
-xy-zzy
"criminal-plan"
"(206)555-1384"
"ab,c"
"9.8"
To be considered a match, the strings must have exactly the same number of
dashes in exactly the same positions. The strings might be of different
length. For example, the following calls should each return true:
sameDashes("lst-has-more characters", "2nd-has-less")
sameDashes("lst-has-less", "2nd-has-more characters")
because the strings each have two dashes and they are in the same positions.
But the following calls should each return false because the longer string
has a third dash where the shorter string does not:
sameDashes("1st-has-more-characters", "2nd-has-less")
sameDashes("1st-has-less", "2nd-has-more-characters")
In solving this problem you may use only methods listed
'aI
s4.icA£»+t,ri‘
^
Y-=
i
,1-0/i‘^’X'
,
si.
i++)5(
r,
ui I
LI 1C
Cl ica L
C\jr^