CSC 120 Midterm #1

CSC 120 Midterm #1
Instructor: Elizabeth Patitsas
February 6, 2014
Last name:
First name:
Your utorid:
Your lab section:
TUESDAY
or
WEDNESDAY
(circle one)
• You have 50 minutes to write the exam.
• You are not permitted any aids, notes, books, or calculators.
• This exam is out of 25 points. 26 points are possible.
• There are 4 five-point questions and 6 one-point questions, for a total of ten questions.
• There are eight pages in this booklet.
• Remember to put your name on the back of the booklet.
• If your name, utorid and lab sections are not on the exam, you will receive 0 on the exam.
• As always, you are expected to adhere to the U of T student code of academic conduct.
• We will deduct marks if we cannot read your writing. We have provided generous amounts of space.
• Good luck! :)
Grade earned
Total
1
5
2
5
3
5
4
5
Short Answer Questions
6
Total
25
1
1
5-point questions. (20 points possible.)
Each question in this section is worth 5 points.
1. Fill in the method fun with numbers. If given a number that is divisible by 3, it will print “Bang”. If
given an integer that is divisible by 2, it will print “Fizz”. And if it is given a number that is divisible
by both 2 and 3, it will print “Fizz\nBang”. (Recall that ‘\n’ creates a new line – see example below).
If given a number that is not divisble by 2 or 3, it won’t print anything.
>>> fun_with_numbers(3)
Bang
>>> fun_with_numbers(2)
Fizz
>>> fun_with_numbers(6)
Fizz
Bang
>>> fun_with_numbers(7)
Your solution should not exceed 5 lines of code (that does not include the docstring). Include a
docstring.
def fun_with_numbers(x):
2
2. What will these print? (1 point each.)
(a) alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(alist[5])
(b) alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(alist[2][0])
(c) alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(3.14 in alist)
(d) alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(57 in alist)
(e) alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
print(len(alist))
3
3. Jill has discovered a new and improbable chemical compound that has the following phase diagram:
The x axis is temperature in degrees Celcius; the y axis is pressure in kPa. Fill in the method phase
that will return the phase as a string given the temperature/pressure. On boundary lines, break ties
as such: SUPERCRITICAL FLUID, VAPOUR, LIQUID, SOLID.
Your solution should not exceed 10 lines of code (that does not include the docstring).
def phase(temperature, pressure):
’’’(number, number) -> str
Return the phase of this improbable chemical as a string
written in ALL CAPS.’’’
4
4. Let’s say you’re given a Julian Date like we had on the assignment. Recall that is a number of days
in fractional time. You want to print it out as DAY HH:MM:SS, so you create helper methods to get
the day out of it, the hours, the minutes, and the seconds.
Fill in the method, frac days to hours, that takes the fractional time in days (a float) and returns
the number of hours.
Your solution should not exceed 5 lines of code (that does not include the docstring). For readability,
no single line of code should contain more than two operations (e.g. multiplication, division, calling a
function).
Usage examples:
>>> frac_days_to_hours(23.5)
12
>>> frac_days_to_hours(22399292.25)
6
def frac_days_to_hours(julian_date):
‘‘‘(float) -> int
Return the number of hours you’d see in a Day Hour:Minute:Second
display after converting a julian date (fractional days)
into discrete time.’’’
5
2
1-point questions. (6 points possible.)
Each question in this section is worth 1 point.
5. In Python, what does the following code print?
x = "4"
y = 3
x = y
y = x - 3
print( type(x/y) )
(a) <class ‘string’>
(b) <class ‘float’>
(c) <class ‘int’>
(d) Instead of printing something, there will be an error.
6. What will this return for funct(5)?
def funct(x):
if x == 1 or 2 or 3:
return True
elif x < 10:
return False
else:
return None
(a) True
(b) False
(c) None
(d) Instead of returning, there will be an error.
7. What is printed by the following statements?
s = "python"
excl = "!"
print(s+excl*3)
(a) python!!!
(b) python!python!python!
(c) pythonpythonpython!
(d) Instead of printing something, you will get an error
6
8. What will the following code print out?
def alter_number(x):
x = x ** 2
x = 3
print( alter_number(x) )
(a) 3
(b) 6
(c) 9
(d) None
9. Which line of code is not logically the same as the others?
(a) if day % 2 == 0
(b) if not (day % 2 == 1)
(c) if not day % 2
(d) if day + 1 % 2 == 1
(e) if day - 1 % 2 == 0
10. Consider the following code:
if not something_that_returns_boolean():
return True
else:
return False
Which replacement for this code will produce the same result?
(a) return False
(b) return True
(c) return not something_that_returns_boolean()
(d) return something_that_returns_boolean()
(e) None of the above
7
.
Last name:
First name:
Your utorid:
8