# CS8—Midterm 1 PRACTICE EXAM #1 Partial answer key and notesE01, 09M, Phill Conrad, UC Santa Barbara 08/17/2009

Name: ________________________________________________________

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

• You are permitted one sheet of paper (max size 8.5x11") on which to write notes
• These sheets will be collected with the exam, and might not be returned

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:

• if a question is worth 10 points, spend no more than 5 minutes on it
• if a question is worth 20 points, spend no more than 10 minutes on it
• if a question is worth 40 points, spend no more than 20 minutes on it
• etc.

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, and assume that the statement `import math` has already been included in the Python source file.

There are many correct answers to this question. Here is one:

```def volumeOfCone(height, radius):       return (1.0/3.0) * math.pi * (radius**2) * h```

```def volumeOfCone(height,radius):       return (1//3) * math.pi * (radius ** 2) * h```

The answer above will always return zero. Why?

Another correct answer is this one:

``` def volCone(h,r):      return (math.pi * r * r * h)/3```

Note that this answer is correct, even though it doesn' t have 3.0, because the quantity being divided by the int value 3 is already a float. Why?

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')```

There are many correct answers. We'll solve this one in lecture together.

``` ```

3. (30 pts) Fill in the table below, according to these instructions:
• The first column describes a Python expression
• In the second column, fill in a Python expression that matches the description in the first column
• In the third column, fill in the value of the expression you put in the second column. If that expression involves variables, put the values you assumed for those variables in the fourth column.
• The first row is filled in as an example.

There are many possible answers here, so the ones given below are only examples.

Also, realize that although the questions on your sheet will be identical to these, they may appear in a different order (shuffled).

Description
Expression
Value
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

3**2
9

An expression involving an operator that has a value of type string

"x" + "y"
xy

An expression of type bool (Boolean)

x<5
True
x=2

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

x * 2.0
6.0
x=3

An expression involving one addition operator and one multiplication operator

2+3*4
14
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.

If the blanks are too small, you can write your answers beside them.

(The exact session you have will be similar to this one, but may be slightly different.
The best way to check your answers is to try the session in IDLE yourself, so I have not filled in answers below.)

```>>> 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?

There are many possible answers here. If you want to see if your answer is good, write it down and bring it to office hours. You may also want to read about function calls and function definitions in your text:

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

It's hard to circle in HTML, but I've used bold and background color to indicate the correct answers below, and in the no cases, to provide some explanation.

Note that on your paper, the questions will be the same, but they may appear in a different order (shuffled), so be sure to check the questions as well as the answers.

 `lunch_break` Circle:       yes        no `onceAnd4All` Circle:       yes        no `twice bitten` Circle:       yes        no (spaces are not permitted) `3timesALady` Circle:       yes        no (may not start with a digit) `def` Circle:       yes        no (def is a keyword—I don't expect you to memorize all the keywords, but this is one that you should know from defining functions) `my-friend` Circle:       yes        no (you can have an underscore, but a hyphen is prohibited, because it could be confused with a minus sign. These are two legal variables, and this is a python expression, saying "my minus friend")

Total points: ?