COSC 1436 - Ch 7
11. If you call the index method to locate an item in a list and the item is not found, this happens. a. A ValueError exception is raised. b. An Invalidindex exception is raised. c. The method returns - 1. d. Nothing happens. The program continues running at the next statement.
A ValueError exception is raised.
5. This will happen if you try to use an index that is out of range for a list. a. A ValueError exception will occur. b. An IndexError exception will occur. c. The list will be erased and the program will continue to run. d. Nothing-the invalid index will be ignored.
An IndexError exception will occur.
A file object's writelines method automatically writes a newline ( '\n' ) after writing each list item to the file. T or F
False
Assume list1 references a list. After the following statement executes, list1 and list2 will reference two identical but separate lists in memory: list2 = list1 T or F
False
Lists in Python are immutable. T or F
False
8. In python, the index of the first element of the list is 1. T or F
False it's 0
7. When the * operator's left operand is a list and its right operand is an integer, the operator becomes this. a. The multiplication operator b. The repetition operator c. The initialization operator d. Nothing-the operator does not support those types of operands.
The repetition operator
4. This is the last index in a list. a. 1 b. 99 c. 0 d. The size of the list minus one
The size of the list minus one
A list can be an element in another list. T or F
True
Tuples in Python are immutable. T or F
True
You can use the + operator to concatenate two lists. T or F
True
The del statement deletes an item at a specified index in a list. T or F
True my_list = [1, 2, 3, 4, 5] print('Before deletion:', my_list) del my_list[2] print('After deletion:', my_list) >>>> Before deletion: [1, 2, 3, 4, 5] After deletion: [1, 2, 4, 5]
8. This list method adds an item to the end of an existing list. a. add b. add to c. increase d. append
append
9. Which of the following is not a property of tuples? a. tuples are immutable data structures b. tuples do not support functions like len, min, max c. tuples can be accessed just like lists d. processing tuples is faster than lists
b. tuples do not support functions like len, min, max - tuples are immutable (cannot be changed) - tuples DOES support functions like len, min, max - processing a tuple is faster than a list
12. The method used to write an entire list to a file is known as: a. writelines b. writeline c. writelists d. writelist
b. writeline cities = ['New York', 'Boston', 'Atlanta', 'Dallas'] outfile = open('cities.txt', 'w') outfile.writelines(cities) outfile.close() >>>New YorkBostonAtlantaDallas
3. In Python, lists are: a. mutable data structures b. static data structures c. dynamic data structures d. both a. and c.
d. both a. and c. A list is an object that contains multiple data items. Lists are mutable, which means that their contents can be changed during a program's execution. Lists are dynamic data structures, meaning that items may be added to them or removed from them. You can use indexing, slicing, and various methods to work with lists in a program.
1. This term refers to an individual item in a list. a. element b. bin c. cubbyhole d. slot
element
2. This is a number that identifies an item in a list: a. element b. index c. bookmark d. identifier
index ex: print(my_list[0], my_list[1], my_list[2], my_list[3])
6. This built-in function returns the highest value in a list. a. min() b. max() c. minimum() d. maximum
max()
10. Assume the following statement appears in a program: mylist = [ ] Which of the following statements would you use to add the string 'Labrador' to the list at index 0? a. mylist[0] = 'Labrador' b. mylist.insert(0, 'Labrador') c. mylist. append ( 'Labrador' ) d. mylist.insert('Labrador', 0)
mylist.insert(0, 'Labrador')
13. The file object method returns a list containing the file's contents: a. to_list b. getlist c. readline d. readlines
readlines
Which of the following statements creates a tuple? a. values [1, 2, 3, 4] b. values {1, 2, 3, 4} c. values ( 1) d. values ( 1,)
values ( 1,) ex: my_tuple = (1, 2, 3, 4, 5) print(my_tuple) >>>(1, 2, 3, 4, 5)