CS8—Midterm 2
E02, 09M, Phill Conrad, UC Santa Barbara
09/01/2009

Name: ________________________________________________________


Umail Address: __________________________________@ umail.ucsb.edu

Please write your name only on this page. That allows me to grade your exams without knowing whose exam I am grading.

This exam is closed book, closed notes, closed mouth, cell phone off,
except for:

There are 100 points worth of questions on the exam, and you have 85 minutes to complete the exam.

A hint for allocating your time—on your first pass through the exam:

If you do that, you'll complete your first pass thorugh the exam in 50 minutes, and still have 35 minutes to


  1. (30 pts) With this exam, you should have received two separate handouts.

    One has code from lab03—the code for ithPointOnUnitCircleX and ithPointOnUnitCircleY, drawPolygon and drawStar.

    The other has diagrams for the ithPointOnUnitCircle,and the output of a new function, drawBisPolygon, which stands for draw bisected polygon.,

    The code for drawBisPolygon appears below, but it is incomplete. Finish the code.

    You may assume that this code gets added at the bottom of the code you were given on the handout, so you do NOT have to write import math, or import cTurtle, or anything like that, and you may call the functions already defined in the code on the handout—in fact, you will need to do this to solve the problem in the space given.

    (It's ok if you go outside the lines a little bit.)

    # drawBisPolygon (turtle, float, int) => void
    # consumes:
    #    t: a turtle (that was already constructed) we'll use to draw the polygon
    #    r: the radius of the polygon
    #    n: the number of sides the polygon has
    # produces: nothing
    # side-effect:
    #    if n is not an even number, nothing is drawn.
    #    Otherwise:
    #      the turtle draws a polygon of n sides, centered at 0,0, exactly
    #      as in drawPolygon.
    #    Then, lines are drawn bisecting the polygon between every pair of points
    #      that are exactly opposite one another.
    
    def drawBisPolygon(t,r,n):
    
        # @@@         Put a line of code here that returns immediately without
        #             drawing anything if the number of points is not an even
        #             number.
        
      

    # @@@ Put in code to draw the polygon. # Hint: you can draw it with one line of code---you don't need # the whole for loop thing.

    # @@@ Now write code to connect the dots for all the bisecting lines # Hint: the code will be similar to the code from drawStar














































  2. (30 pts) Write a Python function definition for a funciton called numRoots that takes three parameters, a, b and c, which are the coefficients of a quadratic equation such as axbx = 0. The function should return either 0, 1 or 2 indicating whether the function has 0, 1 or 2 real roots, according to the following rules:


    Your function should pass these test cases (which you do not need to recopy):
    check_expect("numRoots(1,2,3)",numRoots(1,2,3),0)
    check_expect("numRoots(1,2,-3)",numRoots(1,2,-3),2)
    check_expect("numRoots(1,4,4)",numRoots(1,4,4),1)
    

  3. Questions on this page deal with whether functions are void or fruitful, as defined in our textbook.


    1. (2 pts) Refer back to the function ithPointOnUnitCircleX() from the handout.

      This function is (circle one):           void            fruitful

    2. (2 pts) Refer back to the function numRoots() that you wrote as the answer to question 2.
      If you write this function according to the directions given, this function should be:

      (circle one):           void            fruitful

    3. (2 pts) The function check_expect() is one we've been using throughout the course—the usual definition is given below.

      This function is (circle one):           void            fruitful

      def check_expect(test,check,expect):
          if (check == expect):
              print "Test " + test + " passed."
          else:
              print "Test " + test + " failed: expected " + str(expect)  + \
                    " but I got " + str(check)
      
      
            
    4. (4 pts) Only one of the following is a true statement.

      Which one? Circle:       (1)         (2)         (3)

      (1) Only void functions can be tested by check_expect()

      (2) Only fruitful functions can be tested using check_expect()

      (3) You can test both void and fruitful functions using check_expect()


  4. (20 pts)


  5. (10 pts) Listed below is an session from IDLE, showing interaction between a user and the Python shell. The response from the shell at various points has been replaced with blanks. Fill in the blanks with what the Python shell would print.

    If the blanks are too small, you can write your answers beside them.
    >>> bus = "MTD"
    >>> lines = [15,23,24,27]
    >>> times = (0,15,30,45)
    >>> bus[0]
    ______
    >>> bus[-1]
    ______
    >>> bus[1:]
    ______
    >>> lines[0]
    ______
    >>> lines[1:2]
    ______
    >>> type(lines[1:2])
    ______
    >>> type(lines[1])
    ______
    >>> type(times)
    ______
    >>> len(times[1:])
    ______
    >>> times[1:3]
    ______
    >>>

End of Exam

Total points: ?