Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Python Practice Exam 3

The document is a practice exam for CS 100, featuring multiple choice questions and programming tasks related to Python concepts such as string manipulation, boolean logic, and turtle graphics. It includes questions that require the application of functions, loops, and data structures like lists and dictionaries. Additionally, there are programming assignments that involve creating functions to analyze area codes and write statistics to files.

Uploaded by

colorslash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python Practice Exam 3

The document is a practice exam for CS 100, featuring multiple choice questions and programming tasks related to Python concepts such as string manipulation, boolean logic, and turtle graphics. It includes questions that require the application of functions, loops, and data structures like lists and dictionaries. Additionally, there are programming assignments that involve creating functions to analyze area codes and write statistics to files.

Uploaded by

colorslash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

CS 100 2019F

Midterm 02: Practice Exam 3

Questions 1-10 are multiple choice. 4 points each.


Question 1
nurseryRhyme = 'Rain rain go away'
vowelCount = 0
n = -1
while vowelCount < 3:
if nurseryRhyme[n] in 'aeiouAEIOU':
vowelCount += 1
n -= 1
print(n)

a) -7
b) -6
c) 11
d) 12
e) none of the above

Question 2
def splitLength(sentence):
result = 0
for s in sentence.split():
if len(s) > 3:
result += 1
return result

lisa = 'Dad, are you listening to me?'


print(splitLength(lisa))

a) 1
b) 2
c) 3
d) 4
e) none of the above

Question 3
boolList = [False, False or False, False and False, not False, not not False]
sum = 0
for b in boolList:
if not b:
sum += 1
print(sum)

a) 1
b) 3
c) 5
d) 7
e) none of the above
Question 4
s = 'abc'
print(s[0:] + s[-2:2] + s[:1])

a) abcaba
b) abcabb
c) abcabc
d) IndexError: string index out of range
e) none of the above

Question 5
def mirrorOnWheels(s):
prev = 0
for current in range(1, len(s) - 1):
prev = current - 1
next = current + 1
if s[prev] == s[next]:
break
else:
continue
return 0
return prev

s = 'Good decision!'
print(mirrorOnWheels(s))

a) 0
b) 1
c) 3
d) 8
e) none of the above

Question 6
mixture = {1:[1, 2, 0], 2:[2, 0, 1], 0:[0, 1, 2]}
print(mixture[2][2])

a) 0
b) 1
c) 2
d) 3
e) none of the above
Question 7
noOddHeroes = []
heroes = ['superman', 'batman', 'aquaman']
for hero in heroes:
if len(hero) % 2 == 0:
noOddHeroes.append(hero)
print(noOddHeroes)

a) []
b) ['superman']
c) ['superman', 'batman']
d) ['superman', 'batman', 'aquaman']
e) none of the above

Question 8
candyOnStick = 'lolli lolli lolli lollipop lollipop'
wordList = candyOnStick.split('i')
d = {}
for word in wordList:
if word not in d:
d[word] = 1
else:
d[word] += 1
print(len(d))

a) 2
b) 3
c) 4
d) 5
e) none of the above

Question 9
def oldMcDonald(farm):
result = 0
for animal in farm:
if animal[0] in farm[animal]:
result += 1
return result

farm = {'cow':'moo', 'duck':'quack', 'cricket':'chirp'}


print(oldMcDonald(farm))

a) 0
b) 1
c) 2
d) 3
e) none of the above
Question 10
def analyzer(fileName):
inputFile = open(fileName)
line = inputFile.readline()
inputFile.close()
return line.count(',')

quotes = open('alice.txt', 'w')


quotes.write('Now, here, you see, it takes all the running\n')
quotes.write('you can do, to keep in the same place.\n')
quotes.close()
print(analyzer('alice.txt'))

a) 1
b) 3
c) 4
d) ValueError: I/O operation on closed file.
e) none of the above
Question 11A (8 points)
Write a function named triangle that uses turtle graphics to draw an equilateral triangle of specified
size. An equilateral triangle is a triangle in which all three sides are equal and all three internal angles are
also equal and are 60 degrees each.
The function triangle takes two parameters:
1. t, a turtle that is used for drawing
2. side, the length of a side

The function triangle should draw an equilateral triangle at the initial position and orientation of t, and
should leave t with the same position and orientation on exit. Turtle t is initially at one of the three
points of intersection and is oriented in the direction in which one of the sides should be drawn. Do not
make any assumptions about the initial up/down state of the turtle.
For full credit you must use a loop for repeated operations.
The following is correct sample input and output.
import turtle
snappy = turtle.Turtle()
triangle(snappy, 100)

Question 11B (12 points)


Write a function named umbrella that uses turtle graphics and the function triangle in question 11A to
draw a sequence of triangles of specified size, position and orientation.
The function umbrella takes 4 parameters:
1. t, a turtle used for drawing
2. side, the length of a side
3. rotation, the angle that each triangle is rotated counterclockwise relative to the previous triangle
4. count, the number of triangles to draw

If umbrella is called by the following code, this would be correct output:


import turtle
snappy = turtle.Turtle()
umbrella(snappy, 100, 60, 6)
Question 12 (20 points)
Write a function named analyzeAreaCodes that takes a list of telephone numbers and computes the
count of telephone numbers that are in each area code.
The function analyzeAreaCodes takes one parameter:
• phones, a list of telephone numbers where each number is a string of the form 999-999-9999. The
leftmost three digits are the area code. Thus the telephone number 123-456-7890 has area code 123.
The function analyzeAreaCodes returns a dictionary where each key is an area code in list phones, and
the corresponding value is the count of telephone numbers in the area code. For example, the following
would be correct output:
>>> phones = ['982-867-5309', '800-649-2568', '979-606-0842', '982-779-311']
>>> print(analyzeAreaCodes(phones))
{'982': 2, '800': 1, '979': 1}

Question 13 (20 points)


Write a function named lineStats that finds the number of consonant and vowel letters on each line of a
file and writes those statistics to a corresponding line of a new file, separated by a space.
Definitions: A vowel letter is one of: a, e, i, o, u, along with the corresponding uppercase letters. A
consonant letter is not a vowel letter.
The function lineStats takes two parameters:
1. inFile, a string, the name of an input file that exists before lineStats is called
2. outFile, a string, the name of an output file that lineStats creates and writes to

Important: The input file contains only upper and lower case letters and white space (no punctuation
marks or other special characters).
For example, if the following is the content of the file mary.txt:
Mary had a little lamb
Its fleece was white as snow

And everywhere that Mary went


The lamb was sure to go

The following function call:


>>> inFile = 'mary.txt'
>>> outFile = 'maryStats.txt'
>>> lineStats(inFile, outFile)

should create the file maryStats.txt with the content:


12 6
14 9
0 0
17 8
11 7

You might also like