Exam Information
Exam details
-
The exam will be held on Thursday 12/19, 9:00-11:00am at Photonics 206.
-
The exam will be closed notes/closed books.
-
You may not use any electronic devices during the exam.
-
The exam will include questions similar to the ones posed as in-class practice problems.
-
In addition, there will be questions that ask you to write explain the output of a function or section of code, or to trace a function, similar to the problems from the homework.
Material covered
The exam will be comprehensive on the content of MF703. Approximately 25% of the exam will focus on material from weeks 1-6 of the course, i.e., fundamentals of programming in Python:
- Variables and arithmetic
- Strings and lists – indexing and slicing, iterations with loop
- Functions – parameters and return values
- If/else logic
- Recursion (processing numbers, strings, lists), and producing results as numbers, strings, or lists
- List comprehensions
- Iterative programming with definite loop (for) and indefinite loop (while), accumulator variables
- Lists and 2d lists
You should review all of the material from the practice problems for the midterm exam.
The remainder of the exam (approximately 75%) will be focused on material from weeks 7-12 of the course, including:
- User Defined Classes; methods and data attributes; operator overloading
- Inheritance and Polymorphism; overriding methods
- Numpy: Array and Matrix, indexing and slicing, element-wise and linear algebra operations
- Pandas: Series and DataFrame, indexing and slicing, descriptive statistics, cumulative operations
- SQL: SELECT queries only, including JOINs, WHERE, GROUP BY, ORDER BY.
You should review all of the material from the assignments, as these topics are fair game for the exam.
Practice Problems
Python Questions:
-
Create a Python class called
Triangle
. The constructor for this class should take two arguments,base
andheight
, and store those values in appropriately named attributes. In addition, you should add a method calledarea
that computes and returns the area of the triangle. (The area of a triangle is 0.5 times the product of its base and height.) For example:>>> tri = Triangle(3, 4) >>> tri.area() 6.0
-
Add a method to your
Triangle
class that enables you print aTriangle
object in a readable way. For example:>>> tri = Triangle(3, 4) >>> print(tri) triangle with base 3 and height 4
-
Add a method to your
Triangle
class that will allow you to use the==
operator to test if twoTriangle
objects are equal–i.e., if they have the same base and height. For example:>>> tri1 = Triangle(3, 4) >>> tri2 = Triangle(3, 4) >>> tri3 = Triangle(4, 3) >>> tri1 == tri2 True >>> tri1 == tri3 False
-
Write a function called
main
that creates three triangle objectstri1
(with base 3 and height 4),tri2
(with base 6 and height 6), andtri3
(also with base 3 and height 4). The function should print the three objects and their areas. Next, it should test whethertri1
andtri2
are equal and report the result. Finally, it should test whethertri1
andtri3
are equal and report the result. Your function should take full advantage of theTriangle
methods you have written. Here is the desired output:>>> main() tri1: triangle with base 3 and height 4 (area = 6.0) tri2: triangle with base 6 and height 6 (area = 18.0) tri3: triangle with base 3 and height 4 (area = 6.0) tri1 and tri2 are not equal tri1 and tri3 are equal
-
Write a subclass of
Triangle
calledEquilateralTriangle
. Its constructor should take a single parameterside
representing the length of a side. However, the new class should not have any new attributes. Rather, it should use the attributes that are inherited fromTriangle
, and you should initialize those attributes by calling the superclass constructor and passing it the appropriate values. (You should approximate the height of the triangle as 0.866 times the side length.) For example:>>> tri1 = EquilateralTriangle(6) >>> print(tri1) triangle with base 6 and height 5.196
-
Override the appropriate method in
EquilateralTriangle
so that printing anEquilateralTriangle
object produces an output that looks like the following:>>> tri1 = EquilateralTriangle(6) >>> print(tri1) equilateral triangle with side 6
-
Consider the following python code, which contains no errors:
>>> a = np.array(range(60)).reshape((6,10)) >>> a.reshape((5,12))
What is the value of the following expressions?
i.
a[-1][0]
ii.
a[0][-1]
iii.
a[4][2]
iv.
a[-1]
-
Consider the following python code, which contains no errors:
>>> a = np.array(range(5)) >>> b = np.array([3 for i in range(5)])
What is the value of the following expressions?
i.
a + b
ii.
a * b
iii.
a -= b
-
Consider the following python code, which contains no errors:
>>> a = np.matrix(range(5)) >>> b = np.matrix([3 for i in range(5)])
What is the value of the following expressions?
i.
a + b
ii.
a * b
iii.
a -= b
-
Assume that we have two variables
xs
andys
, that arenumpy.arrays
. Usingnumpy
data structures and linear algebra operations (no loops), write the following functions to calculate:a.
mean(xs)
b.
variance(xs)
c.
stdev(xs)
d.
covariance(xs, ys)
-
Consider the following python code, which contains no errors:
>>> s = pd.Series(data=np.random.random(size=5), index=['a','b','c','d','e'])
a. Write a statement to extract the element
a
by its index.b. Write a statement to extra the elements with indices
a
,b
,c
. -
Consider the following python code, which contains no errors:
>>> df = pd.DataFrame(data=np.random.random(size=(20,3)), index=range(20), columns=['a', 'b', 'c'])
a. Write a statement to extract the column
b
.b. Write a statement to extract the values from all columns, rows 5 through 10.
c. Write a statement to extract the values from column
b
, rows 5 through 10.d. Write a statement to create a new
DataFrame
containing the values from all columns, rows 5 through 10.e. Write a statement to create a new
DataFrame
containing the values from columnb
.f. Write a statement to create a new
DataFrame
containing the values from columnb
, rows 5 through 10. -
Consider the following python code, which contains no errors, which reads a
.csv
data file into apd.DataFrame
:>>> df = pd.read_csv('SPY.csv') >>> df.index = df['Date'] >>> df = pd.DataFrame(df['Adj. Close'])
a. Write a function
find_min_price_date
that returns the date on which the minimum price occured.b. Write a function which returns a new
DataFrame
containing the same index, the adjusted close price (namedprice
), and a column containing the 30-day rolling average price (namedrolling
). -
Consider the following database table schema:
profiles (profile_id, name, city) images (image_id, profile_id, timestamp, comment)
a. Write a SELECT query to obtain all
images
for which the comment contains the stringfun
.b. Write a SELECT query to obtain all
images
related to theprofile
with the nameJohn Lennon
.c. Write a SELECT query to obtain a count of all
images
for allprofiles
, grouped by thecity
associated with the profile, in descending order by count. For example, the results might contain:New York, 42 Boston, 25 Chicago, 12