Compsci 101, Spring 2012, March 1

By entering your name/net-id below you indicate you are in class on March 1 to answer these questions and that you have answered them. Your name should not appear unless you are in class when answering these questions.
Name_________________   net-id _______   Name_________________   net-id _______


Name_________________   net-id _______   Name_________________   net-id _______

Questions based on the code in StockData.py, use that for answering questions.

  1. When the program is run it finds data for Google stock, which trades under the abbreviation "GOOG", between January 1, 2011 and January 1, 2012. Looking at the code, which string(s) must be changed to find stock data for January 1, 2010 to Januaray , 2012.

    1. Change "20110101"

    2. Change "20120101"

    3. Change both "20110101" and "20120101"

  2. The stock symbol for Apple is "AAPL", what changes in the program to find the stock data for Apple between January 1, 2011 and January 1, 2012.

    1. Change "GOOGLE" to "APPLE"

    2. Change "GOOG" to "AAPL"

    3. Change "GOOG" to "APPLE"

  3. The output printed by the program is partially shown below:
    [('2011-01-03', 604.35, 2365200), ('2011-01-04', 602.12, 1824500), ...]
    
    This is obtained by printing the variable data. What are the types for data and data[0], respectively.

    1. list and list

    2. list and tuple

    3. tuple and list

    The data for each day includes the date, the closing price, and the number of shares traded on that day, e.g., ("2011-01-03", 604.35, 2365200) shows that the closing price was $604.35 and that 2,365,200 shares were traded. The maximum closing price in a list obtained in the program can be determined using this list comprehension:

    max_price = max(s[1] for s in data) What's the type of the variable s used in this expression?
    1. list

    2. string

    3. tuple

  4. The code below is meant to find the day on which the stock has the greatest closing price for all information stored in data in the program: max_price = 0 for info in data: if info[1] > max_price: max_price = info[1] max_day = info[0] print max_day Why is max_price initialized to 0?

    1. So that the first time through the loop the if statement will trigger, e.g., the expression tested will be true

    2. Because variables should always be initialized to 0 or "" or [] depending on type.

    3. Because indexes start at zero in Python (and some other languages)

  5. Which of the following list comprehensions and code could also be used to find the date on which the closing price of the stock is highest and store the value in max_date (more than one may be correct)

    1.   mp = max(s[1] for s in data)
        vals = [tup[0] for tup in data if tup[1] == mp]
        max_date = vals[0]
      

    2.   mp = max(s[1] for s in data)
        index = [i for i,tup in enumerate(data) if tup[1] == mp]
        max_date = data[index[0]]
      

    3.   [tup[0] for tup in data if tup[1] == max(s[1] for s in data)][0]
      

  6. Write code to find the average closing price for all the stocks stored in a list of tuples as in this program. Store the average in avg_close