# CS8—Midterm 1 E01, 09M, Phill Conrad, UC Santa Barbara 08/18/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 85 minutes to complete the exam.

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

• 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.

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

• revisit any questions where you need more time

1. (10 pts) Inside every triangle (it doesn't have to be any particular kind of triangle), it is possible to inscribe a circle as shown in the three below. The circle touches each side of the triangle at a single point.

The radius of this circle can be found with the following formulas, found at mathforum.org:

Write the definition for a Python function to perform this calculation.

• The name of the function should be innerRadius
• The parameters to the function should be the values of a, b and c.
• The function return the radius (i.e. the value of r)
• You may write "import math" above your function defintion, and use the function math.sqrt() to make the square root calculation.

2. (10 pts) Here is the definition of a Python function that draws the letter L, using the cTurtle module. At the moment, the width and height of the L are hard coded: width is 50 and height is 100.

 ```def drawL(t): t.left(90) t.forward(100) t.backward(100) t.right(90) t.forward(50) t.backward(50)```

Rewrite the function, generalizing it so that it can make a letter L of any height or width.

Note that the function starts with the Turtle at the lower left hand corner of the L, facing right, and ends with the Turtle at the same spot. Your function should also do that, even after you generalize it.

Important Note: Focus only on rewriting the `drawL` function. Don't worry about things like `import cTurtle`, or `fred=cTurtle.Turtle("turtle") `or any of that other stuff, which is not relevant to the problem I'm asking you solve here. You won't get any extra points for including that stuff—in fact, points may be deducted. I only want the re-written `drawL` function.

3. (10 pts) You are writing some software for a bookstore. The shipping charge for an order is \$0.50 per pound, with a minimum charge of \$3.00.

Write a Python function called computeShipping that takes the weight of an order as a parameter, and returns the shipping charge for that order. Be sure that you never return a shipping charge less than \$3.00.

Hint: you'll need to use an if/else statement.

Note: you are returning the dollar amount as a floating point number. Don't worry about the \$ or the number of digits after the decimal point—you may assume that some other part of the program is taking care of formatting the number for printing.

Your function should pass these test cases:

```check_expect("computeShipping test 1",computeShipping(1.5),3.0) check_expect("computeShipping test 2",computeShipping(5.0),3.0) check_expect("computeShipping test 3",computeShipping(6.0),3.0) check_expect("computeShipping test 4",computeShipping(6.1),3.05) check_expect("computeShipping test 5",computeShipping(8.0),4.0) ```

1. (5 pts) Write an expression that involves the concatenation operation

2. (4 pts) In the following expression, how many operands are there, and what are they?

2 + 3

3. (4 pts) What is the value of the expression

56 % 2

4. (4 pts) What is the value of the expression

7 // 5

5. (4 pts) What is the value of the expression

7 / 5

6. (4 pts) What is the value of the expression

7 % 5

7. (10 pts) Assuming x is a Python variable with a numeric value, show two different ways in Python to express the value of x2

8. (5 pts) Write a Python expression involving the variable x and the number 3 that will evaluate to either True or False

9. (5 pts) What values will be printed by this code?

`for i in range(4):   print (i)           `
10. (5 pts) What values will be printed by this code?

`for i in range(4,8,2):   print (i)           `

5. (10 pts) Suppose you are interviewing for a programming job at AppFolio, a local Goleta employer. You have heard that they use Test Driven Development. (This is absolutely true, by the way.) The interviewer says:

You have on your resume that you learned about Test Driven Development in CS8 at UCSB. So, I want to see if you really understand. Suppose I said to you: "what's the big deal about these 'stub functions'. Suppose I'm writing a function for the area of a triangle. I know perfectly well that I should write: return 0.5*base*height—so what is the point of writing a 'wrong answer' such as return -1 the first time around. Isn't that just a big waste of time?

Even if you personally still feel that it is "a big waste of time"—you need the job, and you know that he wants to see that you understand the ideas behind "test driven development". You can sense that he/she does not feel it is "a big waste of time", and she/he wants to see if you understand why.

How do you answer the question to maximize your chances of getting a job offer?

6. (10 pts) Your friend, who is also taking CS8, says to you:

I'm still not clear on the difference between "formal parameter" and "actual parameter". Can you explain it to me, in plain English?

How do you answer your friend? Note that your bus is coming in a couple of minutes, so you don't have time for a long-winded explanation. Make sure your explanation is complete, and precise (correct) but also consise (i.e. relatively short).

Total points: ?