CS_100_Spring_2018_Final
CS_100_Spring_2018_Final
There are 13 questions on this test. Record your answers to the first 10 questions by circling a letter
below. Answer questions 11, 12 and 13 on the attached pages. We have also provided separate scratch
pages for writing trial code for the programming problems and tracing code for the multiple choice
problems. Work on the scratch pages will not be graded. The value of each question is
There is a penalty of one point if your name is not clearly legible and one point if your section is not
correct. If you are not sure of your section, check it on the signout sheet when you hand in your exam.
Allocate your time accordingly. You may receive partial credit for questions 11, 12 and 13. Answer them
as completely as you can. If you finish early, use the extra time to double check your work. When you are
done, hand in this answer sheet (including programming question solutions), the scratch pages, and sign
the exam attendance sheet.
You may use the summary of Python language elements that is provided. You may not use other notes,
books or electronic devices. If you have brought a cell phone or other mobile device you must leave it
with the proctor during the exam.
Good luck!
Q1 a b c d e
Q2 a b c d e
Q3 a b c d e
Q4 a b c d e
Q5 a b c d e
Q6 a b c d e
Q7 a b c d e
Q8 a b c d e
Q9 a b c d e
Q10 a b c d e
Write code for Questoos A aod B here. Use vertcaa aioes for iodeotatoo.
Write code for Questoo 1 here. Use vertcaa aioes for iodeotatoo
Write code for Questoo 3 here. Use vertcaa aioes for iodeotatoo.
Multiple Question 1
def analyzeString(s):
d = {}
for character in s:
chrCount = s.count(character)
if chrCount not in d:
d[chrCount] = [character]
else:
d[chrCount].append(character)
return d
print(analyzeString('indeed'))
a. 2
b. 4
c. {1: ['i', 'n'], 2: ['d', 'e', 'e', 'd']}
d. {'i': 1, 'n': 1, 'd': 2, 'e': 2}
e. none of the above
Question 2
def notIn(a, b):
indexA = 0
while True:
if b not in a[indexA:]:
return a[indexA:]
else:
indexA += 1
return ""
firstString = 'aha'
secondString = 'a'
print(notIn(firstString, secondString))
Question 3
dTest = {1:'one', 0:'zero', 'two':2}
print(dTest[0][1])
a. z
b. o
c. ['one','zero']
d. ['zero','one']
e. none of the above
Question 4
def syms(text):
wordList = text.split()
rtnList = []
for i in range(len(wordList)):
if wordList[i] == wordList[-i-1]:
rtnList.append(wordList[i])
return rtnList
s = 'all for one and one for all'
print(syms(s))
Question 5
burns = ["a", "man's", "a", "man", "for", "all", "that"]
out = []
for i in range(len(burns)-2):
if burns[i] == burns[i+2]:
continue
elif burns[i][0] in burns[i+2]:
out.append(burns[i])
break
else:
out.append(burns[i+2])
print(out)
a. []
b. IndexError: list index out of range
c. ["man"]
d. ["man's"]
e. none of the above
Question 6
input = 'boohoo'
output = ""
for i in range(2):
output += input[i]
for j in range(1):
output += input[j]
print(output)
a. bb
b. bbo
c. bbobb
d. bboobo
e. none of the above
Question 7
beatles50 = "You say you want to have a revolution"
def makeDict(t):
wordList = t.split()
d = {}
for word in wordList:
final = word[-1]
if final not in d:
d[final] = [word]
else:
d[final].append(word)
break
return d
print(len(makeDict(beatles50)))
a. 7
b. 8
c. 1
d. 2
e. none of the above
Question 8
bools = [False, True, True and not False, not False, True or not True,
True or False]
output = 1
for expr in bools:
if expr:
output *= 2
else:
output += 1
print(output)
a. 8
b. 11
c. 64
d. 96
e. none of the above
Question 9
For this question assume that a file named len.txt exists and has the
following content
def repeatCount(inFile):
inF = open(inFile)
text = inF.read().split()
words = []
repeats = 0
for word in text:
if word not in words:
words.append(word)
else:
repeats += 1
inF.close()
return repeats
print(repeatCount('len.txt'))
a. 2
b. 3
c. 12
d. 13
e. none of the above
Question 10
karl = 'they have a world to win'
freq = {}
for thing in karl.split():
freq[len(thing)] = karl.count(thing)
print(len(freq))
a. 5
b. 6
c. 24
d. TypeError: object of type 'int' has no len()
e. none of the above
Question 11A (12 points)
In a university we structure course offerings into sections (e.g. CS100_002). Each section has a
list of zero or more students enrolled in it. Write a definition line for a class named Section and a
one-line doc string that describes what a section is.
Write definitions for the following methods (functions) in the Section class:
1. An initialization method. The initialization method should:
o Take a single parameter of type string, section_id, and assign it as variable
section_id of the section being created
o Create a variable of type list, enrolled_students, for the section being created and
initialize it to the empty list
2. A method named enroll. The method enroll should take the name of a student as a string
parameter and add that student to the list of students for that section. You may assume
that every name is in First Last format and that every name is unique.
3. A method named is_enrolled. This method should take the name of a student as a string
parameter. If the student is in the section’s list of enrolled students, is_enrolled should
return True, otherwise it should return False.
The function inverse should compute and return a dictionary in which each key is a course and
the associated value is a list of students enrolled in that course.
The function fileStats should read and analyze each line of the input file and write two statistics,
separated by a space, about the line to a corresponding line of the output file.
fileStats('monthStats.txt', 'monthStatsOut.txt')
10 4
62
42