Quiz 2
Points Possible: 100
Date: Thursday, October 10th
Background
This quiz is based on the topics covered in weeks 4 through 6. There are 3 sections — short answer questions, tracing, and a programming problem. Students are allowed to use the Thonny IDE for Section III only. Please see the grading rubric for the programming problem. Scratch paper and a simple calculator are allowed.
Submission
- Submit a
quiz2.txt
file on Blackboard with your answers. - Submit any Python code from the Thonny IDE on Blackboard.
Section I (20 points)
1. How many times does the following loop iterate?
i = 0
while i <= 40:
print(i)
i = i + 2
- A. 20
- B. 22
- C. 21
- D. 40
2. What sequence is generated by range(2, 20, 2)
- A. 2,4,6,8,10,12,14,16,18,20
- B. 2,4,6,8,10,12,14,16,18
- C. 2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18
- D. 4,6,8,10,12,14,16,18
3. What happens when you run this Python program?
balance = compute_balance(100, 3000)
print(balance)
def compute_balance(deposit, balance):
return(deposit + balance)
- A. The program prints: 3100
- B. NameError: name ‘compute_balance’ is not defined
- C. NameError: name ‘balance’ is not defined
- D. NameError: name ‘deposit’ is not defined`
4. Convert the following for
loop statement to a while
loop:
sum = 0
for i in range(1, 20):
sum = sum + i
print(sum)
5. Assume, age = 24
, height = 6
not (age > 18 or height == 6)
What is the value of the expression?
- A. True
- B. False
6. Find the error in the following Python code:
Line 1: def function1(y):
Line 2: if x > y:
Line 3: print("x is greater")
Line 4:
Line 5: function1(2)
- Which line number has an error.
- Explain in one sentence, what is the error.
7. What will be the output of the following?
sum_val = 0
for i in range(1, 10, 2):
sum_val += i
print(sum_val)
- A. 120
- B. 25
- C. 20
- D. None of the above
8. What sequence is generated by range(24, 4, -4)
- A. 4,6,8,10,12,14,16,18,20,22,24
- B. 24,20,16,12,8,4
- C. 24,20,16,12,8
- D. 4,8,12,16,20,24
9. Consider the following Python program.
def find_min(num1, num2, num3):
min_val = min(num1, num2, num3)
return min_val
print(find_min(2, 3, 5))
What will be the output?
- A. 2
- B. 3
- C. 5
- D. Error
10. Assume a = 1
, b = 1
, c = 3
, d = 4
, e = 5
:
( (a > c) or ( (c + 1 < e) and (c - b > a) ) )
What is the value of the expression?
- A. True
- B. False
Section II (30 points)
Trace the execution of the following program by filling in a tracing table. You can draw the tracing table on a piece of paper and include an image file (tracing.png
) on Blackboard.
You may use the following guide for reference:
result = 1
for n in range(4):
result = result * n
print(result)
Section III (50 points)
Programming question:
- You can use the Thonny Python IDE for writing the program.
Write a module that contains the following two functions:
# Converts from feet to meters
# Returns the result in meters
def foot_to_meter(foot):
# Converts from meters to feet
# Returns the result in feet
def meter_to_foot(meter):
The formulas for the conversion are:
\(foot = \frac{meter}{0.3048}\)
\(meter = 0.3048 \times foot\)
Test your functions by writing code that invokes these functions to display the following tables:
Feet Meters
1 0.3048
2 0.6096
3 0.9144000000000001
4 1.2192
5 1.524
6 1.8288000000000002
7 2.1336
8 2.4384
9 2.7432000000000003
10 3.048
Meters Feet
20 65.61679790026247
21 68.89763779527559
22 72.17847769028872
23 75.45931758530183
24 78.74015748031496
25 82.02099737532808
26 85.30183727034121
27 88.58267716535433
28 91.86351706036744
29 95.14435695538057
30 98.42519685039369
In the code used to test your functions:
- Use a loop and call the
foot_to_meter(foot)
function to print the table. Input statements are not required as the loop runs from1
to10
(see the sample table) - Use a loop and call
meter_to_foot(meter)
function to print the table. Input statements are not required as the loop runs from20
to30
(see the sample table) - Save your program as
length.py
and attach it to Blackboard.
Grading Rubric for Section III:
Grading | Points Possible |
Appropriate header and comments | 5 |
Function 1 | 10 |
Function 2 | 10 |
Test Code | 15 |
Print output | 10 |