# Quiz 1#

## Question 1#

An `if`

{.python} statement can have how many
`elif`

{.python} parts?

[ ] 0 \

[x] Unlimited, i.e., 0 or more \

[ ] 1 \

## Question 2#

Consider the Boolean expression
`not (p or not q)`

{.python}. Give the four following values
in order, separated only by spaces:

the value of the expression when `p`

{.python} is
`True`

{.python}, and `q`

{.python} is
`True`

{.python},

the value of the expression when `p`

{.python} is
`True`

{.python}, and `q`

{.python} is
`False`

{.python},

the value of the expression when `p`

{.python} is
`False`

{.python}, and `q`

{.python} is
`True`

{.python},

the value of the expression when `p`

{.python} is
`False`

{.python}, and `q`

{.python} is
`False`

{.python},

Remember, each of the four results you provide should be
`True`

{.python} or `False`

{.python} with the
proper capitalization.

Enter answer here:\

`False False True False`

{.python}

## Question 3#

A common error for beginning programmers is to confuse the behavior of
`print`

{.python} statements and
`return`

{.python} statements.

`print`

{.python} statements can appear anywhere in your program and print a specified value(s) in the console. Note that execution of your pythonthon program continues onward to the following statement. Remember that executing a`print`

{.python} statement inside a function definition does not return a value from the function.`return`

{.python} statements appear inside functions. The value associated with the`return`

{.python} statement is substituted for the expression that called the function. Note that executing a`return`

{.python} statement terminates execution of the function definition immediately. Any statements in the function definition following the`return`

{.python} statement are ignored. Execution of your pythonthon code resumes with the execution of the statement after the function call.

As an example to illustrate these points, consider the following piece of code:

```
def do_stuff():
print "Hello world"
return "Is it over yet?"
print "Goodbye cruel world!"
print do_stuff()
```

Note that this code calls the function `do_stuff`

{.python}
in the last `print`

{.python} statement. The definition of
`do_stuff`

{.python} includes two
`print`

{.python} statements and one
`return`

{.python} statement.

Which of the following is the console output that results from executing this piece of code? While it is trivial to solve this question by cutting and pasting this code into CodeSkulptor, we suggest that you first attempt this problem by attempting to execute this code in your mind.

```
Hello world
Is it over yet?
Goodbye cruel world!
Is it over yet?
```

\

```
Hello world
```

\

```
Hello world
Is it over yet?
```

\

```
Hello world
Is it over yet?
Goodbye cruel world!
```

\

```
Hello world
Is it over yet?
```

## Question 4#

Given a non-negative integer `n`

{.python}, which of the
following expressions computes the ten’s digit of
`n`

{.python}? For example, if `n`

{.python} is
123, then we want the expression to evaluate to 2.

Think about each expression mathematically, but also try each in CodeSkulptor .

[x]

`(n % 100 - n % 10) / 10`

{.python}\[x]

`((n - n % 10) % 100) / 10`

{.python}\[ ]

`(n - n % 10) / 10`

{.python}\

`%`

has the same precedence as multiplication and division in Python

Integer division may come handy too: `123 % 100 // 10`

## Question 5#

The function calls `random.randint(0, 10)`

{.python} and
`random.randrange(0, 10)`

{.python} generate random numbers
in different ranges. What number can be generated by one of these
functions, but not the other?

(Refer to the CodeSkulptor documentation .)

By the way, we (and most Python programmers) always prefer to use
`random.randrange()`

{.python} since it handles numerical
ranges in a way that is more consistent with the rest of Python.

Enter answer here:\

10

I recommend that you also get used to the Python documentation: `random`

- functions for integers

## Question 6#

Implement the mathematical function $$ f(x) = -5 x^5 69 x^2 - 47 $$ as a Python function. Then use Python to compute the function values $$f(0)$$, $$f(1)$$, $$f(2)$$, and $$f(3)$$. Enter the maximum of these four values calculated.

Enter answer here:\

```
def f(x):
return -5 * x**5 * 69 * x**2 - 47
max(f(0), f(1), f(2), f(3))
```

`-47`

. (second check: the function is monotonic decreasing for `x >= 0`

)

## Question 7#

When investing money, an important concept to know is compound interest.

The equation $$ FV = PV (1 rate)^{periods} $$ relates the following four quantities.

The

*present value*(*PV*) of your money is how much money you have now.The

*future value*(*FV*) of your money is how much money you will have in the future.The

*nominal interest rate per period*(*rate*) is how much interest you earn during a particular length of time,**before**accounting for compounding. This is typically expressed as a percentage.The

*number of periods*(*periods*) is how many periods in the future this calculation is for.

Finish the following code, run it, and submit the printed number. Provide at least four digits of precision after the decimal point.

```
def future_value(present_value, annual_rate, periods_per_year, years):
rate_per_period = annual_rate / periods_per_year
periods = periods_per_year * years
# Put your code here.
print "$1000 at 2% compounded daily for 3 years yields $", future_value(1000, .02, 365, 3)
```

Before submitting your answer, test your function on the following example.

`future_value(500, .04, 10, 10)`

{.python} should return
745.317442824

Enter answer here:\

`1061.8348011259045`

```
def future_value(present_value, annual_rate, periods_per_year, years):
rate_per_period = annual_rate / periods_per_year
periods = periods_per_year * years
# Put your code here.
return present_value * (1 + rate_per_period) ** periods
print("$500 at 4% compounded every tenth of a year for 10 years yields $", future_value(500, .04, 10, 10))
print("$1000 at 2% compounded daily for 3 years yields $", future_value(1000, .02, 365, 3))
```

Note that exponentation has a higher precedence than multiplication.

## Question 8#

There are several ways to calculate the area of a regular polygon. Given the number of sides, $$n$$, and the length of each side, $$s$$, the polygon’s area is

$$\frac{n s^2}{ 4 \tan (\frac{\pi}{n})}$$

For example, a regular polygon with 5 sides, each of length 7 inches, has area 84.3033926289 square inches.

Write a function that calculates the area of a regular polygon, given the number of sides and length of each side. Submit the area of a regular polygon with 7 sides each of length 3 inches. Enter a number (and not the units) with at least four digits of precision after the decimal point.

Note that the use of inches as the unit of measurement in these examples is arbitrary. Python only keeps track of the numerical values, not the units.

Enter answer here:\

`32.705211996014306`

```
def polygon_area(side_count, side_len):
import math
return side_count * side_len**2 / 4 / math.tan(math.pi/side_count)
print(polygon_area(5, 7))
print(polygon_area(7, 3))
```

## Question 9#

Running the following program results in the error

`SyntaxError: bad input on line 8 ('return')`

.

Which of the following describes the problem?

```
def max_of_2(a, b):
if a > b:
return a
else:
return b
def max_of_3(a, b, c):
return max_of_2(a, max_of_2(b, c))
```

Misspelled keyword

Misspelled variable name

Missing parenthesis

Wrong number of arguments in function call

Misspelled function name

Extra parenthesis

Incorrect indentation

Missing colon \

Incorrect indentation \

## Question 10#

The following code has a number of syntactic errors in it. The intended math calculations are correct, so the only errors are syntactic. Fix the syntactic errors.

Once the code has been fully corrected, it should print out two numbers.
The first should be 1.09888451159. Submit the **second** number printed
in CodeSkulptor . Provide at least four
digits of precision after the decimal point.

```
define project_to_distance(point_x point_y distance):
dist_to_origin = math.square_root(pointx ** 2 + pointy ** 2)
scale == distance / dist_to_origin
print point_x * scale, point_y * scale
project-to-distance(2, 7, 4)
```

Enter answer here:\

`3.84609579056`

Python 2 Codeskulptor`3.846095790563293`

Python 3

```
def project_to_distance(point_x, point_y, distance):
import math
dist_to_origin = math.sqrt(point_x ** 2 + point_y ** 2)
scale = distance / dist_to_origin
# Python 2
# print point_x * scale, point_y * scale
# Python 3:
print(point_x * scale, point_y * scale)
project_to_distance(2, 7, 4)
```