Python Practice Exam 3
Python Practice Exam 3
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
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
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(',')
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)
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