e01 : Midterm Exam
num | ready? | description | exam date |
---|---|---|---|
e01 | true | Midterm Exam | Thu 08/23 09:30AM |
What is covered
- Homeworks 1 and 2
- Textbook Chapter 1 through 4.2
- Lectures 1 through 6
- lab00 through lab03
Remember that you get to bring a cheat sheet! (MUST BE HANDWRITTEN, see syllabus)
Topics
- Python numeric data types and expressions: int, float, bool
- Other types of data: str, list and tuple
- Python objects and classes, and the use of class constructors to create new objects (e.g.,
int(2), turtle.Turtle()
) - Variables and assignment
- Python functions - difference between function definition and function call
- Difference between print vs. return
- Ability to generalize a given function and trace through code involving functions
- Testing with pytest
- Turtle graphics
- for loops
- if/else
- Writing an interactive program using input and print
- Modules,
import
ing, and accessing the things defined in them
Links to past exams
Here are some exams from past offerings of CS8 from other professors.
Please note that this quarter we may have covered MORE MATERIAL or LESS MATERAL than the point at which midterm 1 was given in previous quarters. Also, these courses all used a different textbook, and there were different homeworks and different labs. So your exam may cover much more or much less material. Use these as guides for style of exam rather than precise content.
Fall 2010
- E01: http://www.cs.ucsb.edu/~pconrad/cs8/10F/exams/E01/
- E02: http://www.cs.ucsb.edu/~pconrad/cs8/10F/exams/E02/actualExam
- E03: http://www.cs.ucsb.edu/~pconrad/cs8/10F/exams/E03/actualExam
Summer 2010
- E01: http://www.cs.ucsb.edu/~pconrad/cs8/10M/exams/E01/
- E02: http://www.cs.ucsb.edu/~pconrad/cs8/10M/exams/E02/actualExam
- E03: http://www.cs.ucsb.edu/~pconrad/cs8/10M/exams/E03/actualExam
Summer 2009
- E01: http://www.cs.ucsb.edu/~pconrad/cs8/09M/exams/E01/
- E01: Actual Exam http://www.cs.ucsb.edu/~pconrad/cs8/09M/exams/E01/actualExam/
- E01: Practice Exam 1 http://www.cs.ucsb.edu/~pconrad/cs8/09M/exams/E01/CS8_09M_E01_practiceExam_1.html
- E01: Practice Exam 1, partial answer key and discussion http://www.cs.ucsb.edu/~pconrad/cs8/09M/exams/E01/CS8_09M_E01_practiceExam_1_notes.html
- E02: http://www.cs.ucsb.edu/~pconrad/cs8/09M/exams/E02/
- E03: http://www.cs.ucsb.edu/~pconrad/cs8/09M/exams/E03/
ANSWER KEY FOR Fall 2010, E02, Question 2
>>> squared(7)
49
>>>
Answer: CAN’T TELL. If we name the two versions squaredA
and squaredB
and run them both, we see this:
>>> squaredA(7)
49
>>> squaredB(7)
49
>>>
Same output!
Now, what about this one:
>>> for i in range (4):
squared(i)
0
1
4
9
>>>
This one is much more subtle. As it turns out, the answer depends on whether you define the squared
function in the shell, interactively, or whether you define it in a file before you run!
Suppose you defined it in the shell, like this. Note that the ...
characters are not what you type—those come up automatically when you hit return in the Python Shell while defining a function.
>>> def squaredA(x):
... return x * x
...
>>> def squaredB(x):
... print(x * x)
...
>>>
If you define the functions this way, then the answer is CAN’T TELL! See for yourself:
>>> for i in range(4):
... squaredA(i)
...
0
1
4
9
>>> for i in range(4):
... squaredB(i)
...
0
1
4
9
>>> squaredA(i)
BUT if you define the functions in a file, with File => New File
, then choose Run Module
and then type in exactly
the same thing at the Python prompt, you end up with DIFFERENT OUTPUT!
For example this file:
def squared(x):
print( x * x)
for i in range(4):
squared(i)
produces this output:
>>>
RESTART: /Users/pconrad/github/ucsb-cs8/Lecture7_0822/squaredA_vs_squaredB.py
0
1
4
9
>>>
But this file:
def squared(x):
return x * x
for i in range(4):
squared(i)
produces this output (i.e. NO OUTPUT).
RESTART: /Users/pconrad/github/ucsb-cs8/Lecture7_0822/squaredA_vs_squaredB.py
>>>
This is unsatisfying. But, it’s true. When function calls return a value and nothing is done with that value (i.e. it isn’t stored in a variable, it isn’t passed to another function), the result in the Python Prompt (i.e. the Python REPL) is that the value is printed. But inside a file, that value just disappears.
So, in the end, the answer to the question, as it appeared on the exam, is CAN’T TELL, because clearly the code was being typed in at the Python Prompt (Python REPL).
But, I have to confess that there was a moment when I thought the answer was B. If the code had appeared in a file, the answer would definitely have been B.
Ok, let’s move on.
>>> 2 * squared(3)
18
>>>
This one is easy. Must be A.
Finally:
for i in range(4):
print(squared(i))
This one must be B. NOTE that it does NOT produce any Python error messages (sometimes called “Python Vomit”.) Also note that there are two print statements involved here: the one inside the function definition and the one inside the code we are running.