Quiz 3 | CMSC 105 Elementary Programming - Fall 2024

Quiz 3

Points Possible: 100

Date: Thursday, November 7th

Background

This quiz is based on the topics covered in weeks 8 through 10. There are 3 sections — short answer questions, Python statements, 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 will the following loop run?

lst = [10, 21, 78, -9, -8]

for x in lst:
    if x < 0:
        break

2. Given:

def main():
    x = 12
    lst = [12, 21, 2]
    lst = func(x, lst)
    print("Value at index 0 is", lst[0])
    print("Value at index 1 is", lst[1])

def func(num1, number_list):
    num1 = 10
    number_list[0] = num1
    number_list[1] = num1 * 2
    return number_list

main()

What will be the output?

3. What will be the result of the following code?

my_list = [1, 2, 3, 4, 5]
print(my_list[2:4])

4. What is the output of the following code?

my_dict = {"a": 1, "b": 2}
my_dict["c"] = my_dict["a"] + my_dict["b"]
print(my_dict)

5. What will the following code output?

my_list = [10, 20, 30, 40, 50]
print(len(my_list))

6. Which of the following lines of code will raise an error?

7. What will be the output of the following code?

my_list = [1, 2, 3]
my_list[1] = 4
print(my_list)

8. Which method would you use to get a list of all values in a dictionary?

9. Write an expression that will correctly update the value of “age” to 26 in the dictionary.

person = {"name": "John", "age": 25, "city": "New York"}

10. Write an expression to add a new key-value pair “country”: “USA” to the dictionary person.

person = {"name": "John", "age": 30, "city": "New York"}

Section II (40 points)

Given the following dictionary and list:

d = {"Alona":100, "Bob":78, "Charlie":89, "Eva":55, "John":66}
lst = [12, 32, 33, 5, 7, 8]

Write Python statements to do the following:

Section III (40 points)

Programming question:



Write a program that prompts users to enter a sequence of 5 numbers separated by spaces. If the sequence length is not 5, display an appropriate message and exit. Otherwise, create and print a dictionary containing the count of positive and negative numbers.



Sample run:

Enter 5 numbers separated by spaces: 12 2 -11 -3 3
{'Positive': 2, 'Negative': 3}

Grading Rubric for Section III:

Grading Points Possible
Appropriate header and comments 5
Input 10
Computation 15
Print output 10