Sei sulla pagina 1di 8

CSE

101: Introduction to Computers

Fall 2015

Midterm 2 Practice Problems SAMPLE SOLUTIONS



1. Given the nested Python tuple

my_tuple = ( (16, 15, 14, 13), (12, 11, 10), (9, 8, 7, 6,
5), (4, 3, 2, 1) )

How would you retrieve the value 5 from my_tuple?

my_tuple[2][4]


2. Imagine that board is a Python list of three three-element lists, representing a Tic-
Tac-Toe board. Each list value is a string: either X, O, or (blank). We want to
write an if statement that will examine the contents of the middle (horizontal) row
to see if that row contains three Xs. Write a line of Python code that correctly
implements this test.

if board[1][0] == X and board[1][1] == X and
board[1][2] == X:


3. You have been given the initial Python statements:

s1 = "heuristic"
s2 = "algorithm"

Give the value of the Python expression

s1[2] + s2[5:7]

uit


4. Assume that the variables low and high have been declared and assigned integer
values (and that the value of low is less than the value of high). Write some Python
code that will print out all of the values from low through high that are evenly
divisible by 3, and only those values. For example, if low is 5 and high is 21, your
code should print out exactly 6, 9, 12, 15, 18, and 21.

for i in range(low, high+1):
if i % 3 == 0:
print(i)


5. Given the empty Python dictionary values and an integer value n, write some
Python code that will fill the dictionary with keys from 1 through n. Each key k
should map to the tuple (k, k+1, k+2). For example, values[5] should return the
tuple (5, 6, 7).

Stony Brook University

CSE 101: Introduction to Computers

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?

Stony Brook University

CSE 101: Introduction to Computers

Fall 2015

Verification measures whether a software system performs correctly


according to its specification. Validation determines whether a software
system meets the customers needs or requirements. Of the two, verification is
generally easier to perform.



10. You have been asked to test a program that evaluates test scores. The user will enter
the number of points earned, and the program will print an evaluation: Excellent,
Satisfactory, or Unsatisfactory. The evaluation labels should follow the guidelines
below:

Points
Evaluation
85100
Excellent
7084
Satisfactory
069
Unsatisfactory

If the number of points is greater than 100 or less than 0, the program should print
an appropriate message.

Design a test suite for this program. Each test case should contain an input value and
the expected output for that input. Be sure to test boundary conditions as well as
mid-range values!

Answers will vary, but should include input values at the endpoints of each
range, at least one value well within each of the three ranges, a negative input
value, and a positive input value that is greater than 100.



11. Consider the following list of values: 5, 7, 20, 33, 44, 46, 48, 99, 101, 102, 105

a. How many comparisons will it take to find the value 20 using linear search?
Explain your answer.

Linear search will require 3 comparisons to find 20, because it
examines each value in the sequence in order from first to last (so it
will look at 5, then 7, then 20).


b. How many comparisons will it take to find the value 20 using binary search?
Explain your answer.

Binary search will require 2 comparisons to locate 20, because it
repeatedly examines the middle element of each sub-range (so it will
examine 46, then look to the left and examine 20).


Stony Brook University

CSE 101: Introduction to Computers

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

Stony Brook University

CSE 101: Introduction to Computers

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!

Stony Brook University

CSE 101: Introduction to Computers

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

Stony Brook University

18

100

19

71

54

99

CSE 101: Introduction to Computers

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

Stony Brook University

CSE 101: Introduction to Computers

Fall 2015

on an item, clicking an Add to cart button, entering shipping information,


and entering payment information.




Stony Brook University

Potrebbero piacerti anche