# CS8—Midterm 1 E01, 10F, Phill Conrad, UC Santa Barbara 10/12/2010

Name: ________________________________________________________

Umail Address: __________________________________@ umail.ucsb.edu

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 75 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 25 minutes to

• revisit any questions where you need more time
• check your work.

1. (30 pts) In the left column below, there is a function definition for drawC.
In the right column, there is Python code that uses that function defnition to draw a C,
and a picture of the resulting drawing.

• In the version below, the width and height of the L are hard coded: width is 50 and height is 100
• Also, please note that the turtle ends up exactly where it started, facing the same way, with the pen down.

 ```# function definition def drawC(t): # Move turtle to starting point t.up() t.forward(50); t.left(90); t.forward(100); # Draw the C t.down() t.left(90); t.forward(50); t.left(90); t.forward(100); t.left(90); t.forward(50); # Move back to starting position t.up() t.backward(50) t.down() ``` Sample output for: ```fred = cTurtle.Turtle("turtle"); drawC(fred) ```

Your job—on the next page, rewrite the function definiton so that it can draw a C of any width or height.
Your new function should work for the functions calls below, producing output like that shown:

 `drawC(fred,50,100)` `drawC(fred,25,100)` `drawC(fred,100,50)`

There is space for your answer on the next page.

Helpful hints (how to get full credit:

• The drawC function given starts with the Turtle at the lower left hand corner of the C, facing right, and ends with the Turtle at the same spot. Your function should also do that, even after you generalize it. (Points will be deducted if it doesn't.)

• Focus only on rewriting the `drawC` 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 function definition for the re-written `drawC` function—nothing more, nothing less.

• For full credit, include the comments from the original function definition.

Space for you to write the function definition for `drawC`

``` ```
2. (2 pts) I promised you this question, so here it is:

Which of these is a forward-slash? Circle one:           /                   \

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

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

```for i in range(5,15,3):
print (i)
```

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

6. Answer the following questions about the expression 5*3+1

1. (2 pts) What is the left operand of the * operator?

2. (2 pts) What is the right operand of the * operator?

3. (2 pts) What is the left operand of the + operator?

4. (2 pts) What is the right operand of the + operator?

5. (2 pts) What is the type of the expression? Circle one:

`bool     complex     float     int       str`

7. (36 pts) For each of the expressions below,
enter the value in the second column, and then circle the type in the third column.

For each line, entering the value is worth 2 points, and circling the type is worth 1 point

The first two are done for you as examples of what I'm looking for.

Expression Value Type
3 * 5 15
1 + 1 < 3 True
4 / 10   `bool     complex     float     int       str`
4 // 10   `bool     complex     float     int       str`
10 / 4   `bool     complex     float     int       str`
10 // 4   `bool     complex     float     int       str`
4 % 10   `bool     complex     float     int       str`
10 % 4   `bool     complex     float     int       str`
57 % 2   `bool     complex     float     int       str`
57 % 5   `bool     complex     float     int       str`
5 % 57   `bool     complex     float     int       str`
"12"   `bool     complex     float     int       str`
12.0   `bool     complex     float     int       str`
1 + 1 == 3   `bool     complex     float     int       str`

8. (3 pts) Please convert 45 from decimal to binary

9. (3 pts) Please convert 1000 1010 from binary to decimal.

10. (4 pts) Briefly explain: what is the relevance of the "binary" number system to understanding how computers work?

(One or two sentences is enough if you address the main point—I'm not looking for a long essay.)

Total points: ?