Skip to content

Practice Quiz 3 (Ch 5–6)

Name: ___________________________________

This work complies with the JMU Honor Code. I have neither given nor received unauthorized assistance. I will not discuss the quiz contents with anyone who has not taken the quiz.
Initials: ____________

Written Portion

Question 1  (4 pts)

Evaluate each expression below. If an error occurs, write the word Error. Use double quotes for string values.

cats = ["Luna", "Simba", "Bella", "Charlie", "Milo"]
dogs = {"Hound": 8, "Boxer": 3, "Lab": 6, "Duke": 1}
Expression Result Expression Result
dogs[1] Error len(dogs) 4
len(cats) 5 len(cats[3]) 7
cats[2] "Bella" cats[0][0] "L"
dogs["Boxer"] 3 cats["Simba"] Error

Question 2  (4 pts)

Given the following variables:

author = "Jane Austen"
genres = ["Novel", "Fiction", "Romance"]
title = "Pride and Prejudice"
year = 1813

Write a statement (one line of code) for each description. Your answers must follow PEP 8 style (one space before and after =, one space after comma) as shown in the variables above.

a. Assign an empty dictionary to a variable named books.

Answer:   books = {}   or   books = dict()

b. Assign a tuple containing title and year to a variable named key.

Answer:   key = (title, year)   or   key = title, year

c. Add a new item to the books dictionary, using key for the key and author for the value.

Answer:   books[key] = author

d. Print (on the screen) the third element of the list referenced by the genres variable.

Answer:   print(genres[2])

Question 3  (6 pts)

For each code snippet, determine: (1) how many times the loop runs; (2) what is printed on the screen. Notice that end=" " means each value is printed on the same line, followed by a space.


for n in range(1, 15, 3):
    n += 1
    if n % 2 == 0:
        print(n, end=" ")

How many times the loop runs:   5                   What is printed on the screen:   2 8 14


for c in "Go Dukes!":
    if c.islower():
        print(c, end=" ")

How many times the loop runs:   9                   What is printed on the screen:   o u k e s


picks = (44, -17, 253)
for i, x in enumerate(picks):
    if x > 0:
        print(i, x, end=" ")

How many times the loop runs:   3                   What is printed on the screen:   0 44 2 253


Coding Portion

Write the functions below in a file named practice_quiz3.py. Include a module docstring at the top of the file. You do not need to type the function docstrings.

Function 1  (5 pts)

def names_cap(names):
    """Print the names with capitalized first letters.

    Example:
    >>> names_cap(["mayfield", "Sprague", "Shrestha", "wang", "Chao"])
    Sprague
    Shrestha
    Chao

    Args:
        names(list): list of strings

    Hint: str.isupper() returns True when called on an uppercase string.
    """

Function 2  (5 pts)

def limit_letters(counts, limit):
    """Get letters that don't exceed a count limit.

    Ex: limit_letters({'a': 4, 'b': 2, 'c': 1, 'd': 5}, 3) returns
    {'b', 'c'}, because those letters have a count of 3 or less.

    Args:
        counts (dict): Maps letters to counts (see count_letters).
        limit (int): Maximum value for a letter's count.

    Returns:
        set: Letters having a count of "limit" or less.
    """