0% found this document useful (0 votes)
2 views

Python Practice Exam 2

The document contains a practice exam for CS 100, featuring multiple-choice questions related to Python programming concepts and functions. Questions cover topics such as string manipulation, dictionary operations, turtle graphics, and Pig Latin translation. Each question includes a code snippet and multiple-choice answers, testing the understanding of programming logic and syntax.

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)
2 views

Python Practice Exam 2

The document contains a practice exam for CS 100, featuring multiple-choice questions related to Python programming concepts and functions. Questions cover topics such as string manipulation, dictionary operations, turtle graphics, and Pig Latin translation. Each question includes a code snippet and multiple-choice answers, testing the understanding of programming logic and syntax.

Uploaded by

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

CS 100 2019F

Midterm 02: Practice Exam 2

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


Question 1
def frequencyAnalysis(aStr, i):
d = {}
for element in aStr:
freq = aStr.count(element)
if element not in d:
d[element] = freq
elif aStr.count(element) >= i:
d[element] *= 2
else:
d[element] *= -2
return d[aStr[-1]]

print(frequencyAnalysis('606', 1))

a) 2
b) 4
c) -4
d) 6
e) none of the above

Question 2
def substring(a, b):
s = ''
while True:
if a in b:
continue
elif b in a:
return b
else:
return a
return s

print(substring('ark', 'snark'))

a) no return because this is an infinite loop


b) ark
c) snark
d) '' (the empty string)
e) none of the above
Question 3
def textFunction(text):
words = text.split()
rtnList = []
for idx in range(len(words)-1):
word = words[idx]
if word in words[idx+1:]:
rtnList.append(word)
return rtnList

s = 'I want what I want when I want it'


print(textFunction(s))

a) []
b) ['I', 'want']
c) ['I', 'want', 'I', 'want']
d) ['I', 'want', 'I', 'want', 'I', 'want']
e) none of the above

Question 4
word = 'alakazam'
length = len(word)
accum = ''
for i in range(length):
j = (i+2)%length
if word[i] == word[j]:
break
elif word[i] < word[j]:
accum += word[i]
else:
accum += word[j]
print(accum)

a) '' (the empty string)


b) IndexError: string index out of range
c) no output because it is an infinite loop
d) kkml
e) none of the above

Question 5
d = {1:{'three':3}, 0:'zero', '2':0}
print(d[1][0])

a) 3
b) t
c) zero
d) KeyError: 0
e) none of the above
Question 6
bools = [True, not not True, 7//2 == 1, True and False]
output = 1
for bool in bools:
if bool:
output *= 2
else:
output *= -3
print(output)

a) 1
b) -24
c) 36
d) -54
e) none of the above

Question 7
language = 'Python'
output = ''
for w in language[:1]:
output += w
for x in language[-2:]:
output += x
print(output)

a) PoPn
b) Pon
c) Ponyon
d) PoPnyoyn
e) none of the above

Question 8
tom = "it is error only that shrinks from inquiry"

def tailDict(t):
words = t.split()
d = {}
for word in words:
tail = word[-1]
if tail not in d:
d[tail] = [word]
else:
d[tail].append(word)
return len(d)

print(tailDict(tom))

a) 5
b) 6
c) 8
d) KeyError: ' '
e) none of the above
Question 9
For this question assume that a file named edgar.txt exists and has the following content:
keeping time time time
in a sort of runic rhyme
bells bells bells
bells bells bells
def stat(inFile):
inF = open(inFile)
fileContent = inF.read()
things = fileContent.split()
unique = []
for thing in things:
if things.count(thing) == 1 and thing not in unique:
unique.append(thing)
inF.close()
return len(unique)

print(stat('edgar.txt'))

a) 2
b) 3
c) 7
d) 9
e) none of the above

Question 10
will = ['to', 'be', 'or', 'not', 'to', 'be', 'that', 'is', 'the', 'question']

extreme = 0
for word in will:
if len(word) >= extreme:
extreme = len(word)

print(extreme)

a) 0
b) 2
c) 3
d) 4
e) none of the above
Question 11A (8 points)
Write a function named pentagon that uses turtle graphics to draw a regular pentagon of specified size. A
regular pentagon has five equal sides and five equal internal angles of 108 degrees.
The function pentagon takes two parameters:
1. t, a turtle that is used for drawing
2. side, a number that is the length of a side

The function pentagon should draw a regular pentagon at the initial position and orientation of t. It
should leave t at the same position and with the same orientation on exit. Turtle t is initially at one of the
vertices of the pentagon and is oriented in the direction of one of the sides. Rotate the turtle clockwise to
draw successive sides. 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
logy = turtle.Turtle()
pentagon(logy, 40)

Question 11B (12 points)


Write a function named pentagons that uses turtle graphics and the function pentagon in question 11A to
draw a sequence of pentagons of specified size, and orientation. Begin drawing at the initial location and
direction of the turtle.
The function pentagons takes 5 parameters:
1. t, a turtle used for drawing
2. length, an integer that is the length of a side of the first pentagon
3. delta, the increase in length of a side of successive pentagons
4. num, the number of pentagons to draw
5. angle, the angle that each pentagon is rotated clockwise relative to the previous pentagon

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


import turtle
logy = turtle.Turtle()
pentagons(logy, 30, 6, 3, 120)
Question 12 (20 points)
Write a function named filter that filters the elements of a dictionary by removing all key-value pairs
that do not satisfy minimum criteria for keys and values.
The function filter takes three parameters:
1. d, a dictionary in which all keys are strings and all values are integers
2. kFilter, a string
3. vFilter, an integer

The function filter returns a dictionary containing all key-value pairs in d in which the key is less than
kFilter and the value is less than vFilter.

For example, the following would be correct input and output:


>>> d = {'a': 1, 'b': 2, 'c': 3, 'd': 2, 'e': 1}
>>> filtered = filter(d, 'd', 3)
>>> print(filtered)
{'a': 1, 'b': 2}
Question 13 (20 points)
Pig Latin is a language game that translates English words into the made-up language Pig Latin. The
English to Pig Latin translation algorithm is:
• if a word starts with a vowel, append the suffix 'ay' to it. A vowel is one of the letters a, e, i, o, u.

• if a word starts with a consonant (any letter that is not a vowel), move the consonant to the end of
the word and then append the suffix 'ay'.

Examples:

English Pig Latin


duck uckday
me emay
eat eatay
omelet omeletay

Write a function named pigLatin that reads the English words on each line of an input file and writes the
Pig Latin translation of each word, followed by a space ( ' '), to a corresponding line of an output file.
Each line of the input file contains one or more English words, each followed by a space. Each line of the
input file is terminated by a newline ('\n'). Important: The input file contains only lower case letters,
spaces and newline characters.
The function pigLatin takes two parameters:
1. inFile, a string, the name of an input file that exists before pigLatin is called
2. outFile, a string, the name of an output file that pigLatin creates and writes to

For example, if the following is the content of the file english.txt:


egg real
will

The following function call:


>>> inFile = 'english.txt'
>>> outFile = 'latin.txt'
>>> pigLatin(inFile, outFile)

should create the file latin.txt with the content:


eggay ealray
illway

You might also like