1) A set is a container that stores a collection of:
A) unique values
2) One key difference between a set and a list is:
A) Set elements are not stored in any particular order
3) Which statement correctly creates a set named colors that contains the 7 colors in a rainbow?
A) colors = {“red”, “orange”, “yellow”, “green”, “blue”, “indigo”, “violet”}
4) Which statement correctly creates a set named rainbow that contains the 7 colors in a rainbow?
A) colors = [“red”, “orange”, “yellow”, “green”, “blue”, “indigo”, “violet”]
rainbow = set(colors)
5) Which statement correctly creates an empty set flags?
A) flags = set()
6) Which statement correctly identifies the number of elements in the set flags?
A) numflags = len(flags)
7) How can you print all the elements in the set colors each on a separate line?
A) for color in colors :
print(color)
8) In what order are the elements of a set visited when the set is traversed using a for loop?
A) random order
9) Which statement(s) below print the set colors in sorted order?
A) for color in sorted(colors) :
print(color)
10) Which of the following is a possible output after the following code snippet is executed?
names = set(["Jane", "Joe", "Amy", "Lisa"])
names.add("Amber")
names.add("Zoe")
names.discard("Joe")
print(names)
A) {‘Amy’, ‘Lisa’, ‘Jane’, ‘Zoe’, ‘Amber’}
11) Which of the following is a possible output after the following code snippet is executed?
names = set(["Jane", "Joe", "Amy", "Lisa"])
names.add("Amber")
names.add("Zoe")
names.discard("Jim")
print(names)
A) {‘Jane’, ‘Lisa’, ‘Joe’, ‘Amy’, ‘Amber’, ‘Zoe’}
12) What is printed after the following code snippet is executed?
names = set(["Jane", "Joe", "Amy", "Lisa"])
names.add("Amber")
names.add("Zoe")
names.remove("Jim")
print(names)
A) An exception is raised
13) In the following code snippet, what is true about these sets?
names = set(["Jane", "Joe", "Amy", "Lisa"])
names1 = set(["Joe", "Amy", "Lisa"])
names2 = set(["Jane", "Joe"])
A) names2 is a subset of names
14) Given the following code snippet, which statement tests to see if all three sets are equal?
fruit = set(["apple", "banana", "grapes", "kiwi"])
fruit2 = set(["apple", "banana", "grapes", "kiwi"]
fruit3 = set(["apple", "banana", "pears", "kiwi"])
A) if fruit == fruit2 and fruit == fruit3 :
15) What is printed by the following code snippet?
fruit = set(["apple", "banana", "grapes", "kiwi"])
fruit2 = set(["apple", "banana", "grapes"])
fruit3 = set(["apple", "banana", "pears", "kiwi"])
if fruit2.issubset(fruit) :
print("fruit2 is a subset of fruit")
if fruit == fruit3 :
print("fruit and fruit3 are equal")
if fruit != fruit2 :
print("fruit and fruit2 are not equal")
A) fruit2 is a subset of fruit
fruit and fruit2 are not equal
16) What is in the set fruit after the following code snippet?
fruit2 = set(["blueberry", "lemon", "grapes"])
fruit3 = set(["apple", "banana", "pears", "kiwi"])
fruit = fruit2.union(fruit3)
A) {‘blueberry’, ‘lemon’, ‘grapes’, ‘apple’, ‘banana’, ‘pears’, ‘kiwi’}
17) What method can be used to combine two sets in Python?
A) union()
18) What method is used to test if one set is contained entirely within another set in Python?
A) issubset()
19) What method is used to produce a new set with the elements that are contained in both sets?
A) intersection()
20) What method is used to produce a new set with the elements that belong to the first set but not the second?
A) difference()
21) What is the output of the following code snippet?
fibonacci = {1, 1, 2, 3, 5, 8}
primes = {2, 3, 5, 7, 11}
both = fibonacci.intersection(primes)
print(both)
A) {2, 3, 5}
22) What is the output of the following code snippet?
fibonacci = {1, 1, 2, 3, 5, 8}
primes = {2, 3, 5, 7, 11}
both = fibonacci.union(primes)
print(both)
A) {1, 2, 3, 5, 7, 8, 11}
23) Which statement creates an empty set and stores it in x?
A) x = set()
24) What is the value of x after the following code segment executes?
x = {1, 2, 3}
x.add(1)
A) {1, 2, 3}
25) Assume that x is initially the set {1, 2, 3}. Which statement results in x being the empty set?
A) x.clear()
26) What is the value of x after the following code segment executes?
x = {1, 2, 3}
x.discard(4)
A) {1, 2, 3}
27) Consider the following code segment:
primes = {2, 3, 5, 7}
odds = {1, 3, 5, 7}
Which line of code will result in x containing {1, 2, 3, 5, 7}?
A) x = primes.union(odds)
28) What is stored in x at the end of this code segment?
primes = {2, 3, 5, 7}
odds = {1, 3, 5, 7}
x = primes.intersection(odds)
A) {3, 5, 7}
29) Consider the following code segment:
primes = {2, 3, 5, 7}
odds = {1, 3, 5, 7}
Which line of code will result in x containing {1}?
A) x = odds.difference(primes)
30) What method is used to remove elements from a set?
A) discard()
31) Which of the following is NOT true about sets?
A) in a set, elements are stored in the order they are added
32) What method is used to remove all elements from a set?
A) clear()
33) Which statement determines if set x is a proper subset of set y?
A) x.issubset(y) and x != y
34) How can you make sure the elements in a set will be printed in sorted order?
A) Use the sorted function when printing the set
35) What is printed when the following code snippet executes?
names = set(["Jane", "Joe", "Amy", "Lisa"])
names.add("Amber")
names.add("Zoe")
names.clear()
print(names)
A) set()
36) Given the following code snippet, which statement tests to see if names2 is a subset of names?
names = set(["Jane", "Joe", "Amy", "Lisa"])
names2 = set(["Jane", "Joe"])
A) if names2.issubset(names) :
print(names2)
37) Consider the following code segment:
names = set(["Jane", "Joe", "Amy", "Lisa"])
names1 = set(["Joe", "Amy", "Lisa", "Bob"])
names2 = set(["Joe", "Amy", "Lisa"])
Which of the following statements is true?
A) names2 is a subset of names
38) What values will be in the set both after the following code snippet runs?
fibonacci = {1, 1, 2, 3, 5, 8}
primes = {2, 3, 5, 7, 11}
both = fibonacci.difference(primes)
print(both)
A) 1, 8
39) Which statement creates a set of 3 elements and stores it in x?
A) x = {1, 2, 3}
40) Consider the following code segment:
primes = {2, 3, 5, 7}
odds = {1, 3, 5, 7}
x = primes.issubset(odds)
What value will be stored in x after it has executed?
A) False
41) Which statement about lists and sets is correct?
A) In a list the elements are stored in order. In a set they are not stored in any particular order.
42) Which of the following code segments prints red is a color of the rainbow when the set colors contains the string "red"?
A) if “red” in colors :
print(“red is a color of the rainbow”)
43) Which of the following statements creates a dictionary of favorite foods?
A) favoriteFoods = {“Peg”: “burgers”, “Cy”: “hotdogs”, “Bob”: “apple pie”}
44) Which operator tests to see if a key exists in a dictionary?
A) in
45) How do you add items to a dictionary?
A) [] operator
46) How do you remove items from a dictionary?
A) pop method
47) What is printed by the following code segment?
x = dict()
print(x)
A) {}
48) Which of the following statements creates a dictionary with 4 entries?
A) x = {“A”: 1, “B”: 2, “C”: 3, “D”: 4}
49) What are the keys in the following dictionary?
fruit = {"Apple": "Green", "Banana": "Yellow"}
A) Apple and Banana
50) What are the values in the following dictionary?
numbers = {1: 5.5, 2.0: 77, 3: 33}
A) 5.5, 77 and 33
51) Consider the following code segment:
fruit = {"Apple": "Green", "Banana": "Yellow"}
fruit["Plum"] = "Purple"
After it executes, what is the value of fruit?
A) {“Apple”: “Green”, “Banana”: “Yellow”, “Plum”: “Purple”}
52) Consider the following code segment:
fruit = {"Apple": "Green", "Banana": "Yellow"}
fruit["Apple"] = "Red"
A) {“Apple”: “Red”, “Banana”: “Yellow”}
53) Which of the following statements checks to see if the key Apple is already in the dictionary fruit?
A) if “Apple” in fruit :
54) Assume that a dictionary has been initialized as shown below:
fruit = {"Apple": "Green", "Banana": "Yellow", "Plum": "Purple"}
Which statement prints the color of a banana?
A) print(fruit[“Banana”])
55) What is in the fruit dictionary after the following code segment executes?
fruit = {"Apple": "Green", "Banana": "Yellow", "Plum": "Purple"}
fruit.pop("Banana")
A) {“Apple”: “Green”, “Plum”: “Purple”}
56) Which code segment prints only the values stored in the fruit dictionary?
A) for item in fruit.values() :
print(item)
57) Which statement correctly creates a dictionary for converting numbers 1 through 5 to roman numerals?
A) numerals = {1: “I”, 2: “II”, 3: “III”, 4: “IV”, 5: “V”}
58) What is the difference between a list and a dictionary?
A) a list stores individual elements but a dictionary stores key/value pairs
59) Which statement is most correct?
A) A Python dictionary stores associations between keys and values.
60) Which of the following statements creates a duplicate copy of the favoriteFoods dictionary?
A) favoriteFoods2 = dict(favoriteFoods)
61) Consider the following dictionary:
favoriteFoods = {"Peg": "burgers", "Cy": "hotdogs", "Bob": "apple pie"}
What statement will print Peg’s favorite food?
A) print(“Peg’s favorite food is: “, favoriteFoods[“Peg”])
62) How can you access a value stored in a dictionary?
A) A value can only be accessed using its associated key
63) Consider the following code segment:
data = {"A": 65, "B": 66, "C": 67}
print(data["Z"])
What will be displayed when this code segment executes?
A) An error message indicating that an exception was raised.
64) Which of the following statements stores seafood in the food variable if Joe is not a key in the favoriteFoods dictionary?
A) food = favoriteFoods.get(“Joe”, “seafood”)
65) You are creating a program that includes a dictionary where the keys are people’s names and the values are their favorite foods. Which of the following statements adds an entry to the dictionary that indicates that Ravi’s favorite food is chocolate?
A) favoriteFoods[“Ravi”] = “chocolate”
66) Which of the following code segments displays the favoriteFoods dictionary in alphabetical order by name?
favoriteFoods = {"Peg": "burgers", "Cy": "hotdogs", "Bob": "apple pie"}
A) for name in sorted(favoriteFoods) :
print(name, favoriteFoods[name])
67) Which code segment creates a dictionary with keys that are integers and values that are lists?
A) cards = dict()
cards[1] = [“Ace”, “Spades”]
cards[2] = [“Two”, “Spades”]
68) What is returned by a dictionary’s items method?
A) A sequence of tuples
69) Consider the following code segment:
data = {"A": 65, "B": 66, "C": 67}
data["D"] = 68
print(len(data))
What is displayed when this code segment is executed?
A) 4
70) What structure should be used to store a collection of unique values when the order of the elements is not important?
A) A set
71) Which of the following code segments creates a dictionary of lists?
A) days = {}
days[“February”] = [28, 29]
72) What does the following code segment display?
data = {"Jan": 31, "Feb": [28, 29], "Mar", 31}
print(data["Jan"][0])
A) The program raises an exception
73) What does the following code segment display?
data = {"Jan": 31, "Feb": [28, 29], "Mar", 31}
print(data["Feb"][0])
A) 28
74) Assume that you have created a function named drawBarGraph for displaying bar graphs, and that you have stored it in a source file named bargraph.py. How should you import this function into another program so that it can be used as shown below:
bargraph.drawBarGraph(data)
A) import bargraph
75) Consider the following code segment:
pets = {}
pets["Snowball"] = {77, 4.5, "Cat"}
pets["Spike"] = {132, 23.1, "Dog"}
The complex structure pets would best be characterized as
A) A dictionary of sets
76) Consider the following code segment:
x = {}
x["Hello"] = [4, 5]
x["Hello"].append("World")
print(x["Hello"])
What is displayed when it is executed?
A) [4, 5, “World”]
77) Consider the following scenario. You have many friends, each of whom has several phone numbers (home, work, cell, and perhaps others). You want to create a phone book program that stores all of this data. Which of the following data structures would be best suited to this task?
A) a dictionary of lists
78) Consider the following problem:A grocery store carries a wide variety of products that fall into broad categories such as fruit, milk and toys. Each category contains many items. For example, the fruit category contains items like apples, oranges and bananas while the milk category contains items like skim, 2% and chocolate, and the toys category includes items like balls, dolls and trucks. A program for the grocery store needs to be able to add new items to a category, remove items from a category and display all of the items in a category. The order in which the items are displayed is not important. Which container or structure best solves this problem?
A) a dictionary of sets
79) What can specify the address of a web application and the arguments that must be supplied to get it to produce the desired result?
A) The Application Programming Interface (API)
80) What plain text format is commonly used to exchange data between a Python program and a web application?
A) JavaScript Object Notation (JSON)
81) What is the purpose of the loads function in the json module?
A) The loads function converts from JSON format to a Python dictionary.
82) What character is used to separate the address from the arguments in a URL?
A) ?
83) Which of the following statements is not true?
A) Splitting a large program into multiple files can make the program run more quickly.
