CIS 103 Final Exam (Ch. 7 And 9)

25 July 2022
4.7 (114 reviews)
20 test answers

Unlock all answers in this set

Unlock answers (16)
question
Write a statement that associates d with an empty dictionary.
answer
d = { }
Explanation: There is no empty dictionary associated with d.
question
Write a statement that associates d with a one-entry dictionary that maps the str 'answer' to the int value 42.
answer
d = {'answer':42}
question
Given that d refers to a dictionary, write an expression that is the value to which the dictionary maps the key 'answer'.
answer
d['answer']
Explanation: Assuming that the key 'answer' exists in the dictionary, the expression would be: d['answer']
question
Given that d refers to a dictionary, change the value mapped to by the key 'Monty' to 'Python'.
answer
d['Monty'] = 'Python'
Explanation: To change the value mapped to by the key 'Monty' in a dictionary, you would first need to locate the key 'Monty' in the dictionary. Then, you would need to reassign the value mapped to by the key 'Monty' to 'Python'.
question
Given that d refers to a dictionary and that x has been defined, delete the dictionary entry whose key is equal to the value associated with
answer
del d[x]
Explanation: x.The statement delete the dictionary entry whose key is equal to the value associated with x" can be interpreted in two ways. One interpretation is to delete the key-value pair from the dictionary where the key is equal to the value of x. Another interpretation is to delete the key-value pair from the dictionary where the value is equal to the key of x.Assuming the first interpretation, the code would look like this:if x in d: del d[x]This code checks to see if the value of x is a key in the dictionary, and if so, deletes the key-value pair from the dictionary.Assuming the second interpretation, the code would look like this:for k, v in d.items(): if v == x: del d[k]This code iterates through the key-value pairs in the dictionary, and if the value is equal to the key of x, deletes the key-value pair from the dictionary."
question
Assume there is a variable, nobel_peace_prizes, that is associated with a dictionary that maps years to winners of the Nobel Peace Prize for that year and assume it is up-to-date through the year 2005. Write a statement that adds an entry that maps the key 2006 to "Muhammad Yunus and Grameen Bank".
answer
nobel_peace_prizes[2006] = "Muhammad Yunus and Grameen Bank"
Explanation: The statement that would add an entry that maps the key 2006 to Muhammad Yunus and Grameen Bank" would be as follows:nobel_peace_prizes[2006] = "Muhammad Yunus and Grameen Bank""
question
Assume there is a variable, album_artists, that is associated with a dictionary that maps albums to performing artists. Write a statement that inserts the key/value pair: "Live It Out"/"Metric".
answer
album_artists["Live It Out"]="Metric"
Explanation: The statement would be as follows:album_artists[Live It Out"] = "Metric""
question
Given a variable, us_cabinet, that is associated with a dictionary that maps department names to department heads, replace the value "Gonzalez" with "Mukasey" for the key "Justice Department".
answer
us_cabinet["Justice Department"] = "Mukasey"
Explanation: The dictionary us_cabinet is being modified so that the key Justice Department" now points to the value "Mukasey" instead of "Gonzalez"."
question
Write a statement that associates s with the empty set.
answer
s = set()
question
Given that s has been defined, and that the_set that refers to a set, write an expression that whose value is True if and only if the value to which s refers is in the_set.
answer
s in the_set
Explanation: Given that s has been defined, and that the_set refers to a set, the expression (s in the_set) returns True if and only if the value to which s refers is in the_set.
question
Write a statement that defines plist to be the empty list.
answer
plist = [ ]
Explanation:Given that s has been defined, and that the_set refers to a set, the expression (s in the_set) returns True if and only if the value to which s refers is in the_set.
question
Write a statement that defines plist as the list containing exactly these elements (in order): "spam", "eggs", "vikings" .
answer
plist = ["spam", "eggs", "vikings"]
Explanation: A plist is a list containing exactly the elements spam", "eggs", "vikings", in that order."
question
Create a list named tax_rates, consisting of the following five elements: 0.10, 0.15, 0.21, 0.28, 0.31, in that order.
answer
tax_rates = [0.10, 0.15, 0.21, 0.28, 0.31]
Explanation: The tax_rates list consists of the five elements 0.10, 0.15, 0.21, 0.28, and 0.31, in that order. These elements represent the tax rates for the five tax brackets in the United States, which are 10%, 15%, 21%, 28%, and 31%, respectively.
question
Given that plist has been defined to be a list of 30 elements, add 5 to its last element.
answer
plist[-1] += 5
Explanation: The last element of plist is the element at index 29. To add 5 to it, we can set the value at index 29 to be the value at index 29 plus 5.
question
Assume that a list of integers named salary_steps that contains exactly five elements has been defined. Write a statement that changes the value of the last element in the list to 160000.
answer
salary_steps[-1] = 160000
Explanation: The last element in the list can be changed to 160000 with the following statement:salary_steps[4] = 160000
question
Given that plist has been defined to be a list, write an expression that evaluates to True if 3 is an element of plist.
answer
3 in plist
Explanation: The expression 3 in plist" will evaluate to True if 3 is an element of plist."
question
Given a list named play_list, write an expression whose value is the length of play_list.
answer
len(play_list)
Explanation: The expression would be len(play_list)". This would give the number of items in the list named "play_list"."
question
Given that a variable named plist has been defined and refers to a non-empty list, write a statement that associates its first element with 3.
answer
plist[0] = 3
Explanation: The first element of the list is at index 0, so the statement would be plist[0] = 3"."
question
Write a statement that associates t with the empty tuple.
answer
t = ()
Explanation: A tuple is a data structure that is used to store a collection of data. The empty tuple is a tuple that does not contain any data.
question
Write a statement that associates t with a tuple that contains the following elements: 42, 56, 7 .
answer
t = (42, 56, 7)
Explanation: The statement t = (42, 56, 7) associates the tuple containing the elements 42, 56, and 7 with the variable t.