Quiz Review
Background
The quiz will be 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.
Section I (20 points)
1. How many times does the following loop iterate?
i = 0
while i <= 45:
print(i)
i = i + 5
2. What sequence is generated by range(3, 33, 3)
3. What happens when you run this Python program?
tip = compute_tip_amount(100, 15)
print(tip)
def compute_tip_amount(bill_amount, tip_percentage):
return(bill_amount * (tip_percentage / 100))
- A. The program prints: 15.0
- B. NameError: name ‘compute_tip_amount’ is not defined
- C. NameError: name ‘tip’ is not defined
- D. NameError: name ‘bill_amount’ is not defined`
4. Convert the following for
loop statement to a while
loop:
sum = 0
for i in range(100, 0, -2):
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: result = x + y
Line 3: print(result)
Line 4:
Line 5: function1(5)
- 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(4, 16, 3):
sum_val += i
print(sum_val)
8. What will be the output of the following?
num = 24
def print_number():
num = 4
print(num)
print_number()
print(num)
9. Consider the following Python program.
def find_max(num1, num2, num3):
max_val = max(num1, num2, num3)
return max_val
print(find_max(2, 3, 5))
What will be the output?
10. Assume a = 1
, b = 1
, c = 3
, d = 4
, e = 5
:
( (a <= c) or ( (c + 2 == 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:
count = 3
result = 1
while count > 0:
result = result * count
count = count - 1
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 Celsius to Fahrenheit
# Returns the result in Fahrenheit
def celsius_to_fahrenheit(celsius):
# Converts from Fahrenheit to Celsius
# Returns the result in Celsius
def fahrenheit_to_celsius(fahrenheit):
The formulas for the conversion are:
\(Fahrenheit = (Celsius \times 1.8) + 32\)
\(Celsius = \frac{Fahrenheit - 32}{1.8}\)
Test your functions by writing code that invokes these functions to display the following tables:
Celsius | Fahrenheit
--------------------------
0 | 32.0
10 | 50.0
20 | 68.0
30 | 86.0
40 | 104.0
50 | 122.0
60 | 140.0
70 | 158.0
80 | 176.0
90 | 194.0
100 | 212.0
Fahrenheit | Celsius
--------------------------
-20 | -28.9
-10 | -23.3
0 | -17.8
10 | -12.2
20 | -6.7
30 | -1.1
40 | 4.4
50 | 10.0
60 | 15.6
In the code used to test your functions:
- Use a loop and call the
celsius_to_fahrenheit(celsius)
function to print the table. Input statements are not required as the loop runs from0
to100
(see the sample table) - Use a loop and call
fahrenheit_to_celsius(meter)
function to print the table. Input statements are not required as the loop runs from-20
to60
(see the sample table)
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 |