CS5nm Midterm Exam 2
E02, 08F, Phill Conrad, UC Santa Barbara
11/14/2008

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 50 minutes to complete the exam.

A hint for allocating your time:


  1. (30 pts) With this exam, you should have received a separate handout with diagrams on it.
    Please refer to Figures 1, 2, 3 and 4 while answering this question.

    Complete the definition given below for the function makeLogoPoints(), that is called from the drawLogo() function shown in Figure 3. Your answer should pass the test cases given.

    You only need to finish the function definition for makeLogoPoints()—don't worry about adding any more test cases, or any of the import pygame or pygame.init() type stuff that might be needed. Don't worry about the while(True): loop or the pygame.display.update(), or any of that stuff—assume that has already been done correctly. Just focus on this one function definition.

    If you like, you can cross out the two lines after the function header, and write the function a different way—as long as it will work (i.e. it will pass the tests, and work with drawLogo() correctly.)

    def makeLogoPoints(x,y,width):
       points = []
       points.append( (x,y) ) # point 1 from diagram
       
    # Fill in the rest of this function...



























    check_expect("makeLogoPoints(50,100,50)", makeLogoPoints(50,100,50), [(50,100),(100,100),(100,50),(50,50),(50,100),(75,50),(100,100)]) check_expect("makeLogoPoints(150,250,100)", makeLogoPoints(150,250,100), [(150,250),(250,250),(250,150),(150,150),(150,250),(200,150),(250,250)])



  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 drawLogo() from the handout (Figure 3).

      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) Fill in the table below, according to these instructions:
    Expression
    Equivalent Expression
    without not operator
    Value of a=4 and b=3

    not ( a <= 5) and b > 6

    a > 5 and b > 6
    False

    not ( a <= 5 and b > 6)

                                                 
                              

    not (a <= 5 or b > 6)

     

    not( (a%2==0 or b%2!=1) )

     

    (a > 5) and not (b < 7)

     
    not (a == 5) and not (b == 5)    
  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: ?