Midterm 2 Practice With Answers
Midterm 2 Practice With Answers
Midterm 2 Practice With Answers
Fall 2015
6.
7.
8.
9.
Fall 2015
for k in range(1, n+1):
values[k] = (k, k+1, k+2)
Define
a
Python
function
named
volume()
that
computes
and
returns
the
volume
of
a
cone.
Your
function
should
take
two
arguments:
the
radius
(given
in
inches)
and
the
height
of
the
cylinder
(also
given
in
inches).
The
volume
of
a
cone
of
radius
R
and
height
H
is
given
by
the
formula:
Volume
=
(3.14
*
R2
*
H)/3
def volume (radius, height):
numerator = 3.14 * (radius ** 2) * height
return numerator / 3
Define
a
regular
expression
that
will
match
all
binary
strings
(meaning
strings
that
only
contain
1s
and
0s)
that
begin
with
1
and
contain
an
odd
number
of
bits
in
all.
Your
regular
expression
should
only
match
strings
that
satisfy
this
pattern
(for
example,
100
and
10001,
but
not
1010).
1([01]{2})*
or
1(00|01|10|11)*
On
the
Internet,
IP
addresses
are
represented
in
dotted-quad
format,
which
means
that
they
contain
exactly
four
integers,
each
of
which
ranges
from
0255
inclusive,
separated
by
periods
(for
example,
247.125.106.11
is
a
valid
IP
address).
Write
a
regular
expression
that
will
match
a
valid
value
in
dotted-quad
format,
and
nothing
else.
A
single
term
(an
integer
between
0
and
255)
can
be
represented
by
the
regular
expression
((1\d{2})|(2(([0-4]\d)|(5[0-5])))|([1-9]\d)|\d)
so
we
repeat
this
pattern
(followed
by
a
literal
period)
three
times,
plus
once
more
without
the
trailing
period
to
get:
(((1\d{2})|(2(([0-4]\d)|(5[0-5])))|([1-9]\d)|\d)\.){3}((1\d{2})|(2(([0-
4]\d)|(5[0-5])))|([1-9]\d)|\d)
Briefly
explain
the
difference
between
verification
and
validation
in
software
development.
Which
one
is
easier?
Fall 2015
Fall 2015
12. Briefly
explain
why
using
a
linked
list
to
store
a
list
of
values
is
preferable
to
using
an
array.
What
disadvantages,
if
any,
are
there
in
using
a
linked
list
rather
than
an
array?
It
is
much
easier
to
add
values
to
a
linked
list
or
to
delete
values
from
a
linked
list
because
we
do
not
need
to
shift
the
other
elements
of
the
list
around
in
memory;
we
just
update
the
links
for
the
preceding
and
following
elements
of
the
list.
Linked
lists
also
do
not
need
to
be
stored
in
a
contiguous
block
in
memory,
so
they
can
be
expanded
without
restriction,
pending
the
availability
of
free
memory
(they
cannot
be
blocked
in
by
the
presence
of
other
data
structures
in
memory).
Access
to
specific
elements
is
slower
in
a
linked
list
than
in
an
array,
however;
an
array
can
directly
calculate
the
memory
location
where
a
particular
element
is
stored,
while
a
linked
list
must
walk
from
node
to
node
until
the
specified
element
has
been
located.
13. Draw
the
graph
whose
memory
representation
is
given
below,
using
the
notation
from
class
(and
the
textbook).
The
anchor
is
located
at
memory
address
20.
Address
Contents
Address
Contents
10
25
11
26
12
15
27
10
13
30
28
20
29
30
14
15
30
16
31
17
25
32
25
18
30
33
19
34
20
35
21
36
22
15
37
23
38
24
39
Fall 2015
14. What
is
the
out-degree
of
node
E
in
the
graph
above?
Node
E
has
3
outgoing
edges,
for
an
out-degree
of
3.
15. List/describe
at
least
two
cycles
in
the
graph
above.
This
graph
contains
at
least
five
cycles:
A-B-E-A,
B-E-C-B,
C-D-E-C,
D-E-D,
and
A-
B-D-E-A.
16. Draw
the
tree
whose
memory
representation
is
given
below,
using
the
notation
from
class
(and
the
textbook).
The
anchor
(root)
of
the
tree
is
located
at
memory
address
31
(with
the
node
value
42).
Note
that
the
nodes
of
this
tree
hold
integer
values,
not
strings,
so
be
careful
when
you
decode
this
data!
Fall 2015
Address
Contents
Address
Contents
10
54
25
12
11
26
12
63
27
12
13
28
20
14
100
29
15
30
16
36
31
42
17
23
32
18
18
33
25
19
34
18
20
19
35
14
21
36
71
22
29
37
23
99
38
10
24
39
42
12
63
18
100
19
71
54
99
Fall 2015
17. Carefully
examine
the
following
state
diagram:
Assume
that
the
camera
is
inactive,
and
then
the
user
presses
buttons
in
the
following
order:
shutter
button
half-pressed,
shutter
button
fully
pressed,
play
button
pressed,
shutter
button
half-pressed.
What
is
the
resulting
value
of
the
photoCount
variable?
Leaving
the
inactive
state
initializes
photoCount
to
99.
Half-pressing
the
shutter
button
adds
1,
bringing
photoCount
to
100.
Fully
pressing
the
shutter
button
adds
another
1
to
photoCount,
making
it
101.
Pressing
the
play
button
has
no
effect
on
photoCount
(it
affects
displayPhoto
instead).
Finally,
half-pressing
the
shutter
button
adds
1
more
to
photoCount,
for
a
final
value
of
102.
18. Draw
a
UML
class
diagram
for
a
simple
digital
wristwatch.
Wristwatch
hours: int
minutes: int
seconds: int
month: int
day: int
year: int
getTime()
getDate()
setTime()
setDate()
19. Draw
a
UML
use
case
diagram
for
locating
and
purchasing
an
item
on
Amazon.com.
Answers
will
vary,
but
should
include
the
user
typing
in
a
search
term,
clicking
Fall 2015