- A.
dictionary
- B.
set
- C.
None
. You can only build a stack from scratch. - D.
list
- A.
defaultdict
will automatically create a dictionary for you that has keys which are the integers 0-10. - B.
defaultdict
forces a dictionary to only accept keys that are of the types specified when you created thedefaultdict
(such as string or integers). - C. If you try to access a key in a dictionary that doesn't exist,
defaultdict
will create a new key for you instead of throwing aKeyError
. - D.
defaultdict
stores a copy of a dictionary in memory that you can default to if the original gets unintentionally modified.
- A. An abstract class is the name for any class from which you can instantiate an object.
- B. Abstract classes must be redefined any time an object is instantiated from them.
- C. Abstract classes must inherit from concrete classes.
- D. An abstract class exists only so that other "concrete" classes can inherit from the abstract class.
- A. A set is an ordered collection unique items. A list is an unordered collection of non-unique items.
- B. Elements can be retrieved from a list but they cannot be retrieved from a set.
- C. A set is an ordered collection of non-unique items. A list is an unordered collection of unique items.
- D. A set is an unordered collection unique items. A list is an ordered collection of non-unique items.
Q5. Given the following three list, how would you create a new list that matches the desired output printed below?
fruits = ['Apples', 'Oranges', 'Bananas']
quantities = [5, 3, 4]
prices = [1.50, 2.25, 0.89]
# Desired output
[('Apples', 5, 1.50),
('Oranges', 3, 2.25),
('Bananas', 4, 0.89)]
- A.
output = []
fruit_tuple_0 = (first[0], quantities[0], price[0])
output.append(fruit_tuple)
fruit_tuple_1 = (first[1], quantities[1], price[1])
output.append(fruit_tuple)
fruit_tuple_2 = (first[2], quantities[2], price[2])
output.append(fruit_tuple)
return output
- B.
i = 0
output = []
for fruit in fruits:
temp_qty = quantities[i]
temp_price = prices[i]
output.append((fruit, temp_qty, temp_price))
i += 1
return output
- C.
groceries = zip(fruits, quantities, prices)
return groceries
>>> [
('Apples', 5, 1.50),
('Oranges', 3, 2.25),
('Bananas', 4, 0.89)
]
- D.
i = 0
output = []
for fruit in fruits:
for qty in quantities:
for price in prices:
output.append((fruit, qty, price))
i += 1
return output
- A.
def sum(a, b):
# a = 1
# b = 2
# sum(a, b) = 3
return a + b
- B.
def sum(a, b):
"""
a = 1
b = 2
sum(a, b) = 3
"""
return a + b
- C.
def sum(a, b):
"""
>>> a = 1
>>> b = 2
>>> sum(a, b)
3
"""
return a + b
- D.
def sum(a, b):
'''
a = 1
b = 2
sum(a, b) = 3
'''
return a + b
fruit_info = {
'fruit': 'apple',
'count': 2,
'price': 3.5
}
- A.
fruit_info ['price'] = 1.5
- B.
my_list [3.5] = 1.5
- C.
1.5 = fruit_info ['price]
- D.
my_list['price'] == 1.5
- A.
class __init__(self):
pass
- B.
def __init__():
pass
- C.
class __init__():
pass
- D.
def __init__(self):
pass
- A. A decorator is similar to a class and should be used if you are doing functional programming instead of object oriented programming.
- B. A decoratore is a visual indicator to someone reading your code that a portion of your code is critical and should not be changed.
- C. You use the decorator to alter the functionality of a function without the without having to modify the functions code.
- D. An import statement is preceded by a decorator, pyhton knows to import the most recent version of whatever package or library is being imported.
- A. The runtime for searching in a binary search tree is O(1) because each node acts as a key, similar to a dictionary.
- B. The runtime for searching in a binary search tree is O(n!) because every node must be compared to every other node.
- C. The runtime for searching in a binary search tree is generally O(h), where h is the height of the tree.
- D. The runtime for searching in a binary search tree is O(n) because every node in the tree must be visited.