MF 703
Fall 2019

Exam Information

Exam details

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:

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:

You should review all of the material from the assignments, as these topics are fair game for the exam.

Practice Problems

Python Questions:

  1. Create a Python class called Triangle. The constructor for this class should take two arguments, base and height, and store those values in appropriately named attributes. In addition, you should add a method called area 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
    
  2. Add a method to your Triangle class that enables you print a Triangle object in a readable way. For example:

    >>> tri = Triangle(3, 4)
    >>> print(tri)
    triangle with base 3 and height 4
    
  3. Add a method to your Triangle class that will allow you to use the == operator to test if two Triangle 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
    
  4. Write a function called main that creates three triangle objects tri1 (with base 3 and height 4), tri2 (with base 6 and height 6), and tri3 (also with base 3 and height 4). The function should print the three objects and their areas. Next, it should test whether tri1 and tri2 are equal and report the result. Finally, it should test whether tri1 and tri3 are equal and report the result. Your function should take full advantage of the Triangle 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
    
  5. Write a subclass of Triangle called EquilateralTriangle. Its constructor should take a single parameter side 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 from Triangle, 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
    
  6. Override the appropriate method in EquilateralTriangle so that printing an EquilateralTriangle object produces an output that looks like the following:

    >>> tri1 = EquilateralTriangle(6)
    >>> print(tri1)
    equilateral triangle with side 6
    
  7. 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]

  8. 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

  9. 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

  10. Assume that we have two variables xs and ys, that are numpy.arrays. Using numpy 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)

  11. 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.

  12. 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 column b.

    f. Write a statement to create a new DataFrame containing the values from column b, rows 5 through 10.

  13. Consider the following python code, which contains no errors, which reads a .csv data file into a pd.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 (named price), and a column containing the 30-day rolling average price (named rolling).

  14. 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 string fun.

    b. Write a SELECT query to obtain all images related to the profile with the name John Lennon.

    c. Write a SELECT query to obtain a count of all images for all profiles, grouped by the city associated with the profile, in descending order by count. For example, the results might contain:

    New York, 42
    Boston, 25
    Chicago, 12