Quiz 2 | CMSC 105 Elementary Programming - Fall 2024

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

Section I (20 points)

1. How many times does the following loop iterate?

i = 0
while i <= 40:
    print(i)
    i = i + 2

2. What sequence is generated by range(2, 20, 2)

3. What happens when you run this Python program?

balance = compute_balance(100, 3000)    
print(balance)
 
def compute_balance(deposit, balance):
    return(deposit + balance)

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?

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)

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)

8. What sequence is generated by range(24, 4, -4)

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?

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?

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:



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:

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