Programming and Problem Solving - Exam 2 - 2008 | COSC 235, Exams of Computer Science

Material Type: Exam; Professor: Sykes; Class: Programming & Problem Solving; Subject: Computer Science; University: Wofford College; Term: Spring 2008;

Typology: Exams

Pre 2010

Uploaded on 08/18/2009

koofers-user-0rq
koofers-user-0rq 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COSC 235B David A. Sykes
1
Test #2
April 11, 2008
Name: ___________________________ Pledged: ___________________________
Answer all questions. If you can’t remember Python syntax to do something, put in a comment about
what you would like to do.
1. Label three of the truth tables below with the Boolean operators and, or, and not. Label the truth
table that does not match one of these three operators with unknown.
____________ ____________ ____________ ___________
A B A op B
False False False
False True True
True False True
True True True
A op B
False True
False True
True False
True False
A B A op B
False False True
False True True
True False True
True True False
A B A op B
False False False
False True False
True False False
True True True
2. The code for the function findIndexOf() relies on the short-circuit evaluation of Boolean expressions
a. Circle the expression that uses short-circuit evaluation.
b. Explain how the code would fail if short-circuit evaluation was not used.
def findIndexOf(aList, value):
"""Return the lowest index k such that aList[k] equals value if
such a k exists. Return len(aList) otherwise."""
# Look through the list for value, starting at index 0
k = 0
while k < len(aList) and aList[k] != value:
k += 1
# At this point, either k == len(aList) or aList[k] == value
return k
pf3
pf4

Partial preview of the text

Download Programming and Problem Solving - Exam 2 - 2008 | COSC 235 and more Exams Computer Science in PDF only on Docsity!

Test

April 11, 2008

Name: ___________________________ Pledged: ___________________________

Answer all questions. If you can’t remember Python syntax to do something, put in a comment about

what you would like to do.

1. Label three of the truth tables below with the Boolean operators and, or, and not. Label the truth

table that does not match one of these three operators with unknown.

A B A op B False False False False True True True False True True True True

A op B False True False True True False True False

A B A op B False False True False True True True False True True True False

A B A op B False False False False True False True False False True True True

2. The code for the function findIndexOf() relies on the short-circuit evaluation of Boolean expressions

a. Circle the expression that uses short-circuit evaluation.

b. Explain how the code would fail if short-circuit evaluation was not used.

def findIndexOf(aList, value): """Return the lowest index k such that aList[k] equals value if such a k exists. Return len(aList) otherwise."""

Look through the list for value, starting at index 0

k = 0 while k < len(aList) and aList[k] != value: k += 1

At this point, either k == len(aList) or aList[k] == value

return k

3. I wrote the code below and then realized that the file a user specifies might not be able to be

opened. Mark up the code so that it handles the IOException that is raised if the file cannot be

opened by printing the message The file foo.txt cannot be found. If the file cannot be opened, then

the program should not display any line count. Indicate which lines should be indented (if any).

Get the file name from a user and open the file

filename = raw_input("Enter a file name: ") datafile = open(filename)

Print the line preceded by a line number and a tab

lineNumber = 1 # accumulator for line in datafile: print "%4d\t%s" % (lineNumber, line), lineNumber += 1

datafile.close()

4. In the code segment below, identify where:

a. an object is created,

b. a method is called, and

c. a keyword argument is used in a method call.

To paraphrase Zelle, an object knows stuff and it does stuff. List:

d. One thing the object named pt knows: ________________________________

e. One thing the object pt does: ________________________________

win = GraphWin("Sample Window",400, 400)

pt = Point(10, 100)

pt.setFill(color="blue")

pt.draw(win)

win.getMouse()

win.close()

5. Show the output produced by each of the three code segments below.

(a) a,^ b,^ c^ =^ 3,^ 3,^3 if a + b <= c or a + c <= b or b + c < a: print "X" else: if a == b and a == c: print "E" if a == b or a == c or b == c: print "I" else: print "S"

7. Consider a file named popdata2005.txt that contains

population data for the 50 states and the District of Columbia.

The first five lines of the file and the last three lines of the file

are shown at the right. (The ellipses denote lines not shown.)

Complete the definition of the function below. Keep in mind

that some entities have 2- or 3-word names —for example,

District of Columbia, South Carolina, and West Virginia.

def population(name):

"Return the population of the state named by the parameter"

BONUS [only if all other questions have an answer]: Explain what the code below does and how it

works.

flavor = raw_input("Enter a flavor [vanilla]: ") or "vanilla"

Alabama 4557808

Alaska 663661

Arizona 5939292

Arkansas 2779154

California 36132147

West Virginia 1816856

Wisconsin 5536201

Wyoming 509294