Ch7 Exam Questions Lists And Tuples - Test Bank | Python 5e by Gaddis by Tony Gaddis. DOCX document preview.
Starting Out with Python 5e (Gaddis)
Chapter 7 Lists and Tuples
TRUE/FALSE
1. Invalid indexes do not cause slicing expressions to raise an exception.
2. Lists are dynamic data structures such that items may be added to them or removed from them.
3. Arrays, which are allowed by most other programming languages, have more capabilities than Python list structures.
4. A list cannot be passed as an argument to a function.
5. The remove method removes all occurrences of an item from a list.
6. The sort method rearranges the elements of a list so they are in ascending or descending order.
7. The index of the first element in a list is 1, the index of the second element is 2, and so forth.
8. The index -1 identifies the last element in a list.
9. To calculate the average of the numeric values in a list, the first step is to get the total of values in the list.
10. In slicing, if the end index specifies a position beyond the end of the list, Python will use the length of the list instead.
11. In order to create graphs using the matplotlib package, you need to import the pyplot module.
12. To add a descriptive label to the X and Y axes of a graph when using the matplotlib package, you need to import the labels module.
MULTIPLE CHOICE
1. What are the data items in a list called?
a. | data |
b. | elements |
c. | items |
d. | values |
2. When working with multiple sets of data, one would typically use a(n)
a. | list |
b. | tuple |
c. | nested list |
d. | sequence |
3. The primary difference between a tuple and a list is that
a. | you don't use commas to separate elements in a tuple |
b. | a tuple can only include string elements |
c. | a tuple cannot include lists as elements |
d. | once a tuple is created, it cannot be changed |
4. What is an advantage of using a tuple rather than a list?
a. | Tuples are not limited in size. |
b. | Tuples can include any data as an element. |
c. | Processing a tuple is faster than processing a list. |
d. | There is never an advantage to using a tuple. |
5. Which list will be referenced by the variable number after the following code is executed?
number = range(0, 9, 2)
a. | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
b. | [1, 3, 5, 7, 9] |
c. | [2, 4, 6, 8] |
d. | [0, 2, 4, 6, 8] |
6. Which of the following would you use if an element is to be removed from a specific index?
a. | a del statement |
b. | a remove method |
c. | an index method |
d. | a slice method |
7. What is the first negative index in a list?
a. | 0 |
b. | -1 |
c. | -0 |
d. | the size of the list minus 1 |
8. Which method can be used to place an item at a specific index in a list?
a. | append |
b. | index |
c. | insert |
d. | add |
9. Which method or operator can be used to concatenate lists?
a. | * |
b. | + |
c. | % |
d. | concat |
10. Which method can be used to convert a list to a tuple?
a. | append |
b. | tuple |
c. | insert |
d. | list |
11. Which method can be used to convert a tuple to a list?
a. | append |
b. | tuple |
c. | insert |
d. | list |
12. What will be the value of the variable list after the following code executes?
list = [1, 2]
list = list * 3
a. | [1, 2] * 3 |
b. | [3, 6] |
c. | [1, 2, 1, 2, 1, 2] |
d. | [1, 2], [1, 2], [1, 2] |
13. What will be the value of the variable list after the following code executes?
list = [1, 2, 3, 4]
list[3] = 10
a. | [1, 2, 3, 10] |
b. | [1, 2, 10, 4] |
c. | [1, 10, 10, 10] |
d. | Nothing; this code is invalid |
14. What will be the value of the variable list2 after the following code executes?
list1 = [1, 2, 3]
list2 = []
for element in list1:
list2.append(element)
list1 = [4, 5, 6]
a. | [1, 2, 3] |
b. | [4, 5, 6] |
c. | [1, 2, 3, 4, 5, 6] |
d. | Nothing; this code is invalid |
15. This function in the random module returns a random element from a list.
a. choice
b. choices
c. sample
d. random_element
16. This function in the random module returns multiple, nonduplicated random elements from a list.
a. choice
b. choices
c. sample
d. random_element
17. What values will list2 contain after the following code executes?
list1 = [1, 2, 3]
list2 = [item + 1 for item in list1]
a. [1, 2, 3]
b. [2, 3, 4]
c. [6, 7, 8]
d. [[1, 2, 3],[2, 3, 4],[3, 4, 5]]
18. What values will list2 contain after the following code executes?
list1 = [1, 10, 3, 6]
list2 = [item * 2 for item in list1 if item > 5]
a. [2, 20, 6, 12]
b. [10, 6]
c. [20, 12]
d. [[1, 10, 3, 6],[1, 10, 3, 6]]
19. In order to create a graph in Python, you need to include
a. | import matplotlib |
b. | import pyplot |
c. | import matplotlib.pyplot |
d. | import matplotlib import pyplot |
20. What will be the output after the following code is executed?
import matplotlib.pyplot as plt
def main():
x_crd = [0, 1 , 2, 3, 4, 5]
y_crd = [2, 4, 5, 2]
plt.plot(x_crd, y_crd)
if __name__ == '__main__':
main()
a. | It will display a simple line graph. |
b. | It will display a simple bar graph. |
c. | Nothing; plt is not a Python method. |
d. | Nothing; the number of x-coordinates do not match the number of y-coordinates. |
COMPLETION
1. A(n) __________ is an object that holds multiple items of data.
2. Each element in a tuple has a(n) __________ that specifies its position in the tuple.
3. The built-in function __________ returns the length of a sequence.
4. Tuples are __________ sequences which means that once a tuple is created, it cannot be changed.
5. A(n) ___________ is a span of items that are taken from a sequence.
6. Lists are ___________, which means their elements can be changed in a program.
7. The __________ method is commonly used to add items to a list.
8. The __________ exception is raised when a search item is not in the list being searched.
9. The __________ method reverses the order of the items in a list.
10. The __________ function returns the item that has the lowest value in the sequence.
11. A list _____________ is a concise expression that creates a new list by iterating over the elements of an existing list.
12. The __________ package is a library you can use in Python to create two-dimensional charts and graphs.
13. The ___________ function can be used to convert a list to a tuple.