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

CS_100_Spring_2018_Final

The document is an exam paper for CS 100 Spring 2018, consisting of 13 questions, including multiple choice and programming tasks. It outlines the exam format, grading criteria, and instructions for completing the exam, including the use of provided Python summaries. Students are required to write their answers on the answer sheet and submit all materials at the end of the exam.

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

CS_100_Spring_2018_Final

The document is an exam paper for CS 100 Spring 2018, consisting of 13 questions, including multiple choice and programming tasks. It outlines the exam format, grading criteria, and instructions for completing the exam, including the use of provided Python summaries. Students are required to write their answers on the answer sheet and submit all materials at the end of the exam.

Uploaded by

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

CS 100 Spring 2018 Final

Tuesday, May 8, 2018

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

1-10 multiple choice (4 points each)


11-13 programming (20 points each)

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!

Name (print clearly) _______________________________________________________


Student ID _______________________________ Section (see below) _______________
002 MR 11:30; 004 MW 8:30; 006 MR 11:30; 008 MW 8:30; 010 TR 10:00;
012 TR 8:30; 102 W 6:00; 104 T 6:00; 106 R 6:00; 108 R 6:00; 110 F 6:00

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

a. this is an infinite loop that never returns


b. "" (the empty string)
c. a
d. None
e. none of the above

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

a. ['all', 'for', 'one', 'and', 'one', 'for', 'all']


b. ['all', 'for', 'one', 'one', 'for', 'all']
c. ['all', 'for', 'one', 'and']
d. ['all', 'for', 'one']
e. none of the above

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

Everybody knows that the dice are loaded


Everybody rolls with their fingers crossed

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.

Question 11B (8 points)


Assume that correct code for the class Section in Question 11A has been saved in a file named
section.py. Write code that uses these class and method definitions to perform the following
tasks:
1. import the module that defines the class Section
2. create a section with the ID Math111_101
3. enroll students Joe Josephson and Mary Smith in Math111_101
4. check whether Mary Josephson is enrolled in Math111_101 and print the result
Question 12 (20 points)
Write a function named inverse that takes a single parameter, a dictionary. In this dictionary each
key is a student, represented by a string. The value of each key is a list of courses, each
represented by a string, in which the student is enrolled.

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.

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

>>> student_courses = {'Elise':[],'Nelson':['CS100','MATH111'],'Justin':['CS100']}


>>> print(inverse(student_courses))
{'CS100': ['Nelson', 'Justin'], 'MATH111': ['Nelson']}

Question 13 (20 points)


Write a function named fileStats. The function fileStats takes two parameters:

1. inFile, a string that is the name of an input file


2. outFile, a string that is the name of an output file

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.

The two statistics for each line are


1. the number of words
2. the number of digits (digits are the characters in positive integers: 0, 1, 2 …)

For example, if the file monthStats.txt contains the following lines:

February has 28 days or 29 in a leap year


January and March have 31 days
April has 30 days

Then the function call

fileStats('monthStats.txt', 'monthStatsOut.txt')

should produce an output file monthStatsOut.txt with the following content

10 4
62
42

You might also like