1 |
e01 |
CS8 S18 |
Name: | ||
---|---|---|
(as it would appear on official course roster) | ||
Umail address: | @umail.ucsb.edu |
EXAM: e01: Midterm Exam
ready? | date | points |
---|---|---|
true | Tue 04/24 03:30PM |
You may not collaborate on this exam with anyone. If you need to use the restroom, you must leave your cell phone with the exam proctor before leaving the room.
- Write your name at the top of this page AND EVERY ODD NUMBERED PAGE.
- Double check that you turned in ALL pages; look for "End of Exam" on the last page.
- This exam is closed book, closed notes, closed mouth, cell phone off.
- You are permitted one sheet of paper (max size 8.5x11") on which to write notes.
- This sheet will be collected with the exam, and might not be returned.
- Please write your name on your notes sheet.
NOTE: All references to Python on this exam mean Python 3, so you should answer accordingly.
-
(10 pts) Many products in the United States that are sold by weight are measured in ounces, while in most other countries, they are measured in grams. One ounce is approximately 28.3495 grams, and the abbreviation for ounce is
oz
.Given this information, write the definition of a Python function called
oz2g
that takes a parameter calledounces
and returns the corresponding value in grams. -
(5 pts) Now write a function call to this function, as you might type it at the Python prompt, for example, that computes the number of grams equal to 3 ounces.
-
Testing a Python function can be done with the
pytest
module, as we did in lab02 and lab03 in this course. The handout you got with this exam has example code illustratingpytest
test cases.-
(4 pts) Why do we need the
pytest.approx
thing? That is, why can’t we just write:assert cToF(100.0)==212.0
? -
(6 pts) Now consider the
oz2g
function you write earlier. Please write two test cases for that function appropriate for use withpytest
. For convenience, here is a table of two values you may use:ounces grams 2.5 70.8738 3 85.0486 -
(5 pts) When using test cases, we often write a “stub” before writing the correct function definition. Write a function definition for
oz2g
that illustrates this practice—that is, a function definition foroz2g
with a “stub”.
-
-
For the Python code in the left box, write the output in the right box
NOTE: The only difference between the two questions is that in one case,
item
has no quotation marks around it, while in the other case it does ("item"
). That’s on purpose. I want you to tell me what output Python will give in each of these two cases. If you are not sure, write your best guess.(3 pts) the_list = ["CS","Math","PStat"] for item in the_list: print(item, end=" ")
(3 pts) the_list = ["CS","Math","PStat"] for item in the_list: print("item", end=" ")
-
Assuming the following assignment statements have been entered at the Python prompt:
school = "UC Santa Barbara" abbrev = "UCSB" course = "CS8" qtr = "M17" unis = ["UCSB","UCLA","Harvard","Yale","Stanford"] cols = ["red","green","blue"] cols.append("purple") cols.append("blue")
Indicate the value of each of these expressions.
IMPORTANT: Use strict Python syntax. If it’s a string, put it in quotes. Boolean values should be indicated as
False
andTrue
, no quotes, with the capital letter.Expression Result Expression Result (3 pts) 2 + 3 * 5
(3 pts) 10 % 3
(3 pts) abbrev * 2
(3 pts) len(qtr)
(3 pts) qtr[0:2]
(3 pts) unis[1]<unis[2]
(3 pts) course[-1]
(3 pts) len(unis) <= 8
(3 pts) len(cols)
(3 pts) cols[-1][-1]
(3 pts) cols[0]
(3 pts) cols[2][0]
(3 pts) len(cols[1])
(3 pts) "e" in cols[0]
-
(4 pts) A complete Python program that prints out the following message is a very short program indeed:
I can code!
Please write that entire program in the space below. You certainly have more space than you will need.
-
(2 pts) To start work on a Python program you need to type a command that brings up the Integrated Development Environment for Python (version 3). What is this command?
-
(2 pts) What does the Unix
mkdir
command do? -
Indicate with a check ✓ what Python will return as the type of each expression.
Note that when Python reports a type, it reports it with expressions such as
<class 'int'>
and<class 'str'>
.I’m omitting the
<class '___' >
part to save space.-
(2 pts)
type("UCSB")
□str □ int □ float □ list □ tuple □ dict □ bool -
(2 pts)
type(12)
□str □ int □ float □ list □ tuple □ dict □ bool -
(2 pts)
type(1 + 2)
□str □ int □ float □ list □ tuple □ dict □ bool -
(2 pts)
type("24")
□str □ int □ float □ list □ tuple □ dict □ bool -
(2 pts)
type([3] + [4])
□str □ int □ float □ list □ tuple □ dict □ bool -
(2 pts)
type( 3 < 4)
□str □ int □ float □ list □ tuple □ dict □ bool -
(2 pts)
type( {"green":"verde"} )
□str □ int □ float □ list □ tuple □ dict □ bool
-