Name: ________________________________________________________
Umail Address: __________________________________@ umail.ucsb.edu
Please write your name only on this page. That allows me to grade your exams without knowing whose exam I am grading.
This exam is closed book, closed notes, closed mouth, cell phone off,
except for:
There are 100 points worth of questions on the exam, and you have 50 minutes to complete the exam.
A hint for allocating your time:
math.pi as the value of pi, and assume that the statement import math has already been included in the Python source file.def volumeOfCone(height, radius):
return (1.0/3.0) * math.pi * (radius**2) * hdef volumeOfCone(height,radius):
return (1/3) * math.pi * (radius ** 2) * h
def volCone(h,r):
return (math.pi * radius * radius * h)/3
check_expect("slope test 1",slope((0,0),(4,3)),0.75)
check_expect("slope test 2",slope((1,1),(3,3)),1.0)
check_expect_within_tolerance("slope test 3",slope((0,0),(3,4)),1.333333,0.0001)def slope(point1, point2):
return (1.0 * point2[1] - point1[1]) / (point2[0] - point1[0])
1.0 * is necessary to be sure that the numerator is a float before the division takes place. Another solution is to use a type conversion function, as shown in Chapter 3, section 3.1:def slope(point1, point2):
return float(point2[1] - point1[1])/(point2[0] - point1[0])
def computeSlope(x1,y1,x2,y2):
return float(y2-y1)/(x2-x1)
def slope(point1,point2):
return computeSlope(point1[0],point1[1],point2[0],point2[1])def slope((x1,y1),(x2,y2)):
return float(y2-y1)/(x2-x1)
| Description | Expression |
Value |
Variables (if any) |
|---|---|---|---|
An expression involving the addition operator, one variable, and one int value
|
x + 1 |
3 |
x=2 |
An expression involving the ** operator
|
3**2 |
9 |
|
An expression involving an operator that has a value of type string
|
"x" + "y" |
xy |
|
An expression of type bool (Boolean)
|
x<5 |
True |
x=2 |
An expression involving the multiplication operator, one variable,
|
x * 2.0 |
6.0 |
x=3 |
An expression involving one addition operator and one multiplication operator
|
2+3*4 |
14 |
>>> x = 3 >>> y = 7 >>> x * 5 _______ >>> y / 5 _______ >>> y / 7 _______ >>> y / 8 _______ >>> x + 2 * y _______ >>> x + y _______ >>> x = x + 2 >>> x _______ >>> x + y _______ >>> y _______ >>> x _______ >>> "x" _______ >>> "y" + "x" _______ >>> "y" * 3 _______ >>> x * 2 ________ >>> x ** 2 ________ >>>
(10 pts) Suppose your friend is taking CS5NM, and says:
I'm still not 100% clear on the difference between a function call and a function definition. I understand how they look different on the page—I mean, the function defintion has the word def in it, duh—and the function call doesn't. But I'm not sure I understand what they mean. Can you explain it?
How do you explain the difference to your friend in plain english?
There are many possible answers here.
I'll scan some of the best actual answers given by students, ask their permission to put them online (anonymously), and if I'm given permission, I'll post them here later.
lunch_break |
Circle: yes no |
onceAnd4All |
Circle: yes no |
twice bitten |
Circle: yes no (spaces are not permitted) |
3timesALady |
Circle: yes no (may not start with a digit) |
def |
Circle: yes no (def is a keyword—I don't expect you to memorize all the keywords, but this is one that you should know from defining functions) |
my-friend |
Circle: yes no (you can have an underscore, but a hyphen is prohibited, because it could be confused with a minus sign. These are two legal variables, and this is a python expression, saying "my minus friend") |
Total points: ?