CS8—Midterm 1 PRACTICE EXAM #1
E01, 09M, Phill Conrad, UC Santa Barbara
08/17/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. (20 pts) Write a Python function definition that takes the radius and height of a cone as parameters, and returns the volume of that cone. The formula for volume of a cone, in math notation, is v =1/3πr2h. You may use math.pi as the value of pi. You may assume that the statement import math has already been included in the Python source file.



  2. (20 pts) Write a Python function definition for a function called theLongerOne that takes two parameters, each of which is a string. Return the longer of the two strings. If the strings are of equal length, return the first one passed into the function—for example theLongerOne('dog', 'cat') should return 'dog'


    Your function should pass these test cases:

    check_expect("theLongerOne test 1",theLongerOne('mouse','cat'),'mouse')
    check_expect("theLongerOne test 2",theLongerOne('mouse','chicken'),'chicken')
    check_expect("theLongerOne test 3",theLongerOne('cat','dog'),'cat')

  3. (30 pts) Fill in the table below, according to these instructions:
    Description
    Expression
    Value
    values you assumed for variables (if any)

    An expression involving the addition operator, one variable, and one int value

     

    x + 1
    3
    x=2

    An expression involving the ** operator


                              
                  

    An expression involving an operator, where the final expressiong has a value of type string

     

    An expression of type bool (Boolean)

     

    An expression involving the multiplication operator, one variable,
    and where the result is of type float

     

    An expression involving one addition operator and one multiplication operator

     

  4. (15 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.

    Remember that variables retain their previous values.

    If the blanks are too small, you can write your answers beside them.
    >>> x = 3
    >>> y = 7
    >>> x * 5
    
    _______
    >>> y / 5
    
    _______
    >>> y / 7
    
    _______
    >>> y / 8
    
    _______
    >>> x + 2 * y
    _______
    >>> x + y
    
    _______
    >>> x = x + 2
    >>> x
    
    _______
    >>> x + y
    
    _______
    >>> y
    
    _______
    >>> x
    
    _______
    >>> "x"
    
    _______
    >>> "y" + "x"
    
    _______ 
    >>> "y" * 3
    
    _______
    >>> x * 2
    
    ________
    >>> x ** 2
    
    ________
    >>> 
  5. (10 pts) Suppose your friend is taking CS8, and says:

    I'm still not 100% clear on the difference between a function call and a function definition. I understand how they look different on the page—I mean, the function defintion has the word def in it, duh—and the function call doesn't. But I'm not sure I understand what they mean. Can you explain it?

    How do you explain the difference to your friend in plain english?

  6. (5 pts) Which of the following are legal variable names in Python? For each, circle yes or no:


    lunch_break Circle:       yes        no   
    onceAnd4All Circle:       yes        no
    twice bitten Circle:       yes        no
    3timesALady Circle:       yes        no
    def Circle:       yes        no
    my-friend Circle:       yes        no

End of Exam

Total points: ?