Quiz 1b - Logic and Conditionals
Quiz 1b - Logic and Conditionals
Question 1
An if statement can have how many elif parts?
Remember, each of the four results you provide should be True or False with the proper capitalization.
2
False Correct 2.50
Question 3
A common error for beginning programmers is to confuse the behavior of print statements
and return statements.
• print statements can appear anywhere in your program and print a specified value(s) in the console. Note
that execution of your Python program continues onward to the following statement. Remember that
executing a print statement inside a function definition does notreturn a value from the function.
• return statements appear inside functions. The value associated with the return statement is substituted
for the expression that called the function. Note that executing a return statement terminates execution
of the function definition immediately. Any statements in the function definition following
the return statement are ignored. Execution of your Python code resumes with the execution of the
statement after the function call.
3
print "Goodbye cruel world!"
print do_stuff()
Note that this code calls the function do_stuff in the last print statement. The definition of do_stuff includes
two print statements and one return 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
Hello world
Is it over yet?
Goodbye cruel world!
Hello world
Is it over yet?
4
Goodbye cruel world!
Is it over yet?
Question 4
Given a non-negative integer n , which of the following expressions computes the ten's digit of n ? For example, if n is 123, then we want the
expression to evaluate to 2.
Think about each expression mathematically, but also try each in CodeSkulptor.
ü (n // 10) % 10 Correct 4.00 This expression computes the ten's digit correctly.
ü ((n - n % 10) % Correct 4.00 This expression computes the ten's digit correctly. This is a relatively
100) / 10 complicated expression to accomplish the goal.
(n % 10) / 10 Correct 2.00 This expression does not compute the ten's digit correctly.
Remember, try the expression in CodeSkulptor.
5
Total 10.00 /
10.00
Question 5
The function calls random.randint(0, 10) and random.randrange(0, 10) 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() since it handles numerical ranges in a way that is
10 Correct 10.00
6
Question 6
Implement the mathematical function f(x) = -5 x5 + 69 x2 - 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.
69 Correct 10.00
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.
7
• 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.
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) should
return745.317442824.
8
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
¼ n s2 / tan(π/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.
9
Question 9
Running the following program results in the error SyntaxError: bad input on line 8 ('return'). Which of the following describes
the problem?
if a > b:
return a
else:
return b
Misspelled keyword
10
Missing colon
Missing parenthesis
ü Incorrect indentation Correct 10.00 Correct. The body of the function definition
for max_of_3() should be indented, but it is not.
Extra parenthesis
Total 10.00 /
10.00
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.
11
Once the code has been fully corrected, it should print out two numbers. The first should be 1.09888451159. Submit the secondnumber printed
in CodeSkulptor. Provide at least four digits of precision after the decimal point.
project-to-distance(2, 7, 4)
12