sample final 2

advertisement
CS112 Spring 2013
Dr. Kinga Dobolyi
Version A
Final Exam
Do not open this exam until you are told. Read these instructions:
1. This is a closed book exam. No calculators, notes, or other aids are allowed. If
you have a question during the exam, please raise your hand.
2. You must turn in your exam immediately when time is called at the end.
3. 6 problems which add up to 132 points total. 2 hours and 45 minutes. Each
question’s point value is indicated.
4. In order to be eligible for as much partial credit as possible, show all of your work
for each problem, write legibly, and clearly indicate your answers. Credit cannot be
given for illegible answers.
5. After the last page there is paper for scratch work. If you need extra scratch paper
after you have filled these areas up, please raise your hand. Scratch paper must be
turned in with your exam, with your name and ID number written on it, but scratch
paper will not be graded.
6. Please write out the following statement: “I pledge on my honor that I will not give
or receive any unauthorized assistance on this examination.”
7. Fill in the following:
NAME :
SIGNATURE :
G# :
1. Imagine you are working on a team to implement a database for storing
information about types of cars. Answer the following questions:
a. Write a class called Animal, stored in a file called animal.py, that has
the following properties (33 points):
i. Each animal has a color, weight, age, and species.
ii. A constructor that takes as argument three of the attributes above
(no more than three). It initializes those to the incoming values.
The fourth argument should be initialized inside the constructor
to whatever value you want.
iii. All of the fields must ALWAYS be of type string, for the
lifetime of the object.
iv. A to-string method that can be called with a print statement,
which would show the color and weight.
v. A method that returns the age of the animal.
vi. A method to set the age of the animal.
b. Now write a module, saved as driver.py, to create two Car objects
below (you can assign them any reasonable values). Set the mileage of
the first car to 15 miles, and the mileage of the second car to 5 miles.
There should be no errors when I run the module as python
driver.py, assuming your Car class and this file are in the same
directory. (9 points)
c. Let’s imagine you wanted to implement different types of cars. Rather
than write a field for the type, you want to make sub-classes of your Car
class. Show how you would inherit from the base class Car in a new class
Hatchback below (you don’t have to implement anything else in this
question). You may assume this file would be saved as hatchback.py.
(2 points)
2. Show all the output from running the code below (22 points):
num = 3
def func1(var1):
print "in func 1 var1: " + str(var1)
print "type: " + str(type(var1))
print "divide: " + str(var1 / num)
list = []
for i in range(int(var1)):
list.append(i)
return list
def main():
func1(3)
print func1(3.0)
func1(5)
print len(func1(2))
t = func1(2)
print type(t)
print func1(0)
main()
3. Write a function below that calculates the length of an incoming list, without
using any built-in functions. The function should take a single list argument, and
return the length of the list. Your function must not assume anything about the
size of the incoming list, i.e., it could be empty. (8 points)
4. Write an expression that will print out START if the word he is in a string
variable called str, ONCE if the word hello is in str one time, MORE if the
word hello is in str more than one time, and NONE if neither is in str. You
may assume str has already been given a value somewhere else in the code.
You may use the index method of string if needed (i.e.
“kinga”.index(“ing”) returns 1)(13 points)
5. Give the output of the following code. If you think there is an exception at any
point, just write the word EXCEPTION (don’t worry about the details of the
exception): (6 points)
dict = {"a":1, 1:"a"}
print dict["a"]
print dict[1]
dict["a"] = 2
dict["b"] = 3
print dict["a"]
print dict["b"]
print dict["c"]
6. Give the output for the following code: (23 points)
def divide(x="2", y=3):
try:
print "x " + x
print "y " + str(y)
div = int(x) / y
print div
except(ZeroDivisionError), e:
print "cannot divide by zero!"
raise(ValueError(e))
except(Exception), e:
print "something went wrong!"
else:
print "well done"
def main():
try:
print "in main"
divide()
divide(y=2)
divide("7")
print "halfway done divide"
divide("1",1)
divide("1",0)
print "done all divisions"
except(ValueError), e:
print "no"
except (Exception), e:
print "fail"
finally:
print "glad that's over"
main()
7. Short answer (no more than three lines) (15 points)
a. Write an expression to open a file called best.html for reading (it’s in
the current directory), and then print out the entire contents of the file.
b. Write an expression to update the list [2,”kinga”,(1,2)] to contain “king”
instead of “kinga”, or state why this is impossible.
c. Now write an expression to update the tuple (2,”kinga”,(1,2)) to contain
“king” instead of “kinga”, or state why this is impossible.
d. What is the output of the following code?
def test():
print “yes”
print test()
e. What is the output of the following code?
list1 = [1,2,3]
list2 = [list1, 2, 3]
print list2
list1.append(4)
print list2
print list1
(over)
f. Give the output of the following code:
print (True or False)
print (True and False)
print (1 == 1 and 1 != 1)
g. Describe one thing you liked or found useful this semester, and one thing
you did not like or did not find useful this semester.
Download