# CS8—Midterm 2 E02, 10F, Phill Conrad, UC Santa Barbara 11/09/2010

Name: ________________________________________________________

Circle one:          9am     10am     11am     noon

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

1. Two important aspects of programming—in any programming language—are writing functions definitions and function calls.

1. (10 pts) In the space below, write a simple function definition in Python that takes two parameters, each of which is expected to be an integer. The function should returns the smaller of the two integers.

Choose an appropriate name for your function.

Include a check to make sure that both of the parameters are integers.
If they are not both integers, then return the string `"error"`

2. (4 pts) In the space below, write a function call to the function you defined in part (a).
Choose arguments so that the result returned is the number 7.

3. (4 pts) In the space below, write another function call to the function you defined in part (a).
Choose arguments so that the function returns `"error"`

2. (16 pts) Here are two possible definitions for the function `squared`, labelled as definition A and definition B.

Definition "A" Definition "B"
```def squared(x):
return (x * x)
```
```def squared(x):
print (x * x)
```

Below are several examples of output from code that includes a function call to the squared function.
Your job: decide which version of squared was used—or whether it is impossible to tell from the output given.

In each case, simple circle A, B, or "can't tell".

```>>> squared(7)
49
>>>
```
A         B         can't tell
```>>> for i in range(4):
squared(i)

0
1
4
9
>>>
```
A         B         can't tell
```>>> 2 * squared(3)
18
>>>
```
A         B         can't tell
```>>> for i in range(4):
print(squared(i))

0
None
1
None
4
None
9
None
>>>
```
A         B         can't tell
3. (20 pts) Fill in the table below, according to these instructions:
• The first column contains a Python expression involving the not operator
• Directly underneath each expression,
fill in a Python expression that is equivalent to the first, without using the not operator.
• In the second column, fill in the value of the expression, assuming that w, x, y and z have the values shown.

The first row is done for you an an example

under each expression,
fill in an equivalent expression
that doesn't use `not`
Value of expression when
w='foo'; x=2; y=3; z=[1,4]

` not type(z) == list`

type(z) != list

False

`  not (y < z)`

` not ( type(x)==str and len(x)>0)`

` not (type(z)!=list or len(z)==0)`

` not (x > y) and not ((x+y)%2==0)`

` not (z < y and y <= z` )

4. (20 pts) The following Python code contains a function defintion called `makeOneString() `that takes a list of strings, and produces a single string that is the concatenation of all the strings in the list.

The function definition contains five blanks (four points each), marked with __________________.
Fill in these blanks to complete the function definition.

For the empty list, the function should return the empty string.
It is possible to get this result by only filling in the blanks given—no extra if test for empty list is needed.

 ``` def makeOneString(strList): """ strList should be a list of strings (if not, return False) if it is, return a single string which is the concatenation of all strings in the list """ # Fill in the blank with code that checks whether the parameter is a list if _______________________: return False # Initialize an empty string---we using the accumulator pattern on result result = "" # fill in the last part of the for loop line so that # it iterates over the indices (indexes) of the list for i in _________________________________: if (type(strList[i]) != str): ___________________ # do what is appropriate else: _____________________ # do what is appropriate return ____________ # return the completed string check_expect("test 1",makeOneString([]),'') check_expect("test 2",makeOneString(['IV','Rocks']),'IVRocks') check_expect("test 3",makeOneString(['I','Like','UCSB']),'ILikeUCSB') check_expect("test 4",makeOneString('bogus'),False) check_expect("test 5",makeOneString(['Best','Friends',4,'Ever']),False) ```

5. Perform each of the number conversions shown. Be sure you read carefully what base you are converting from and to.

1. (2 pts) Convert 73 from base 10 to binary.

2. (2 pts) Convert 73 from base 8 to binary

3. (2 pts) Convert 73 from base 16 to binary.

4. (3 pts) Only one of these statements is correct—put a check mark next to the correct statements:

___ (i) the three answers above represent three different numbers

___ (ii) the three answers above represent different abstractions for the same number.

5. (4 pts) Explain your answer—why you chose (i) or (ii)—the way you'd explain it to someone that disagreed with you—i.e in a way that would convince him/her that you are correct and he/she is mistaken.

6. Perform each of the number conversions shown. Be sure you read carefully what base you are converting from and to.

Hint: you may need to regroup the bits shown in groups of 3 or 4—or not—depending on the bases involved, and/or add extra zeros at the left hand side.

1. (2 pts) Convert 0 0 1 1 0 1 0 1 1 from binary to decimal

2. (2 pts) Convert 0 0 1 1 0 1 0 1 1 from binary to octal

3. (2 pts) Convert 0 0 1 1 0 1 0 1 1 from binary to hex.

4. (3 pts) Only one of these statements is correct—put a check mark next to the correct statements:

___ (i) the three answers above represent three different numbers

___ (ii) the three answers above represent different abstractions for the same number.

5. (4 pts) Explain your answer—why you chose (i) or (ii)—the way you'd explain it to someone that disagreed with you—i.e in a way that would convince him/her that you are correct and he/she is mistaken.

Total points: ?