WorksheetsChapter 9 Quiz
Total questions: 15
Worksheet time: 5mins
2. Anika is learning about different data structures in programming. She comes across a feature in Python called a dictionary. What is a term commonly used to describe the Python dictionary feature in other programming languages?
A. Associative arrays
B. Closures
C. Sequences
D. Lambdas
3. What would the following Python code print out if Daniel ran it on his computer?
A. candy
B. The program would fail with a traceback
C. -1
D. 0
4. Zoe is trying to debug a piece of Python code that she wrote. She wonders what the output of the code will be when she runs it. What would the following Python code print out?
A. 'candy'
B. The program would fail with a traceback
C. 0
D. -1
5. True or False. William is organizing a list of his favorite books in a dictionary. When he adds the books to the dictionary, they remain in the order in which he added them.
True
False
6. In a data analysis project, Abigail is tasked with analyzing a text file containing customer feedback. What is a common use of Python dictionaries in her program?
A. Splitting a line of input into words using a space as a delimiter
B. Building a histogram counting the occurrences of various strings in a file
C. Computing an average of a set of numbers
D. Sorting a list of names into alphabetical order
7. Zoe is keeping track of how many times each book has been borrowed from her library. Which of the following lines of Python is equivalent to the following sequence of statements assuming that counts is a dictionary?
A. counts[key] = (key in counts) + 1
B. counts[key] = counts.get(key,-1) + 1
C. counts[key] = (counts[key] * 1) + 1
D. counts[key] = counts.get(key,0) + 1
E. counts[key] = key + 1
8. In the following Python, what does the for loop iterate through? Imagine Elijah is writing a program to manage a collection of books stored in a dictionary, where each key represents a book title and the corresponding value represents the author. He wants to understand how the for loop works in this context.
A. It loops through the keys in the dictionary
B. It loops through the integers in the range from zero through the length of the dictionary
C. It loops through all of the dictionaries in the program
D. It loops through the values in the dictionary
9. Noah is organizing a collection of his favorite books in a dictionary object where the keys are the book titles and the values are the authors. Which method in a dictionary object gives Noah a list of the authors in the dictionary?
A. items()
B. keys()
C. all()
D. each()
E. values()
10. Evelyn is trying to access a value in her Python dictionary that contains her favorite fruits. She uses the get() method to retrieve the value associated with the key 'apple'. What is the purpose of the second parameter of the get() method for Python dictionaries?
A. To provide a default value if the key is not found
B. An alternate key to use if the first key cannot be found
C. The value to retrieve
D. The key to retrieve
11. Why does this code produce an error, and how can it be fixed? Imagine Mason is trying to calculate the total sales from a dictionary where the keys are product names and the values are the sales amounts. He writes a code snippet to sum the sales, but it results in an error. What went wrong?
A) sum() requires a list, not a dictionary
B) Use sum(items.values()) to sum values
C) Use sum(items.keys()) to sum keys
D) No changes are needed
12. David is trying to keep track of how many apples he has in his basket using a piece of code. What output will this code produce, and what change will allow it to count properly?
A) {‘apple’: 1}
B) {‘apple’: 0}
C) KeyError
D) None
13. Anika is trying to retrieve both keys and values from a dictionary in her Python code. What correction does she need to make?
A) Add .items() to d in the for loop
B) Use d.keys() instead
C) Change key, value to k, v
D) Replace d with list(d)
14. William is trying to print all the values in his dictionary d that contains the names and ages of his friends. What’s the correction he needs to make?
A) Use d.items() instead of d
B) Change d to d.values()
C) Add print(d[v]) inside the loop
D) Use list(d) instead
15. What will this code output, and why?
