Chapter Seven MULTIPLE CHOICE 1. Which of these is associated with a specific file and provides a way for the program to work with that file? a. Filename b. Extension c. File object d. File variable 2. What do you call the process of retrieving data from a file? a. Retrieving data b. Reading data c. Input data d. Get data 3. What happens when a piece of data is written to a file? a. Data is copied from a variable in RAM to a file. b. Data is copied from a variable in the program to a file. c. Data is copied from the program to a file. d. Data is copied from a file object to a file. 4. Which step creates a connection between a file and a program? a. Open the file. b. Read the file. c. Process the file. d. Close the file. 5. How many types of files are there? a. One b. Two c. Three d. Four 6. A(n) access file is also known as a direct access file. a. sequential b. random c. numbered d. text 7. What type of file access jumps directly to any piece of data in a file without reading the data that came before it? a. Sequential b. Random c. Number d. Text 8. Which mode specifier will open a file but will not let you change the file or write to it?
a. w b. r c. a d. 'e' 9. Which mode specifier will erase the contents of a file if it already exists and create it if it does not exist? a. w b. r c. a d. e 10. Assume that the customer file references a file object, and the file was opened using the w mode specifier. How would you write the string Mary Smith to the file? a. customer_file.write( Mary Smith ) b. customer.write( w, Mary Smith ) c. customer.input( Mary Smith ) d. customer.write( Mary Smith ) 11. When a file has been opened using the r mode specifier, which method will return the file s contents as a string? a. write b. input c. get d. read 12. Which method could be used to strip specific characters from the end of a string? a. estrip b. rstrip c. strip d. remove 13. Which method could be used to convert a numeric value to a string? a. str b. value c. num d. chr 14. Which method will return an empty string when it has attempted to read beyond the end of a file? a. Read b. Getline c. input d. readline 15. What statement can be used to handle some of the run-time errors in a program? a. exception statement b. try statement c. try/except statement
d. exception handler statement TRUE/FALSE 1. True/False: If a file with the specified name already exists when the file is opened, and the file is opened in 'w' mode, then an alert will appear on the screen. 2. True/False: When a piece of data is read from a file, it is copied from the file into the program. 3. True/False: Closing a file disconnects the communication between the file and the program. 4. True/False: Python allows the programmer to work with text and number files. 5. True/False: In Python, there is nothing that can be done if the program tries to access a file to read that does not exist. 6. True/False: The ZeroDivisionError exception is raised when the program attempts to perform a division by zero. 7. True/False: An exception handler is a piece of code that is written using the try/except statement. 8. True/False: If the last line in a file is not terminated with a \n, the readline method will return the line without a \n. 9. True/False: Strings can be written directly to a file with the write method, but numbers must be converted to strings before they can be written. 10. True/False: It is possible to create a while loop that determines when the end of a file has been reached. FILL IN THE BLANK 1. When a program needs to save data for later use, it writes the data in a(n). 2. Programmers usually refer to the process of data in a file as writing data to the file. 3. The term file is used to describe a file to which data is written. 4. The term file is used to describe a file from which the program gets data. 5. A(n) file contains data that has been encoded as text, using a scheme such as ASCII. 6. A(n) file contains data that has not been converted to text. 7. A(n) access file retrieves data from the beginning of the file to the end of the file.
8. A filename is a short sequence of characters that appear at the end of a filename preceded by a period. 9. A(n) gives information regarding the line number(s) that caused an exception. 10. A(n) includes one or more statements that can potentially raise an exception.
Chapter Eight MULTIPLE CHOICE 1. What are the data items in the list called? a. data b. elements c. items d. values 2. Which list will be referenced by the variable number after the execution of the following code? 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] 3. What would you use if an element is to be removed from a specific index? a. del statement b. remove method c. index method d. slice method 4. What is the first negative index in a list? a. 0 b. -1 c. -0 d. Size of the string or list minus one 5. What method can be used to place an item in the list at a specific index? a. append b. index c. insert d. Add 6. What would be the value of the variable list after the execution of the following code? 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]] 7. What would be the value of the variable list after the execution of the following code? list = [1, 2, 3, 4] list[3] = 10 a. [1, 2, 3, 10] b. [1, 2, 10, 4] c. [1, 10, 10, 10]
d. invalid code 8. What method or operator can be used to concatenate lists? a. * b. + c. % d. concat 9. What would be the value of the variable list2 after the execution of the following code? list1 = [1, 2, 3] list2 = list1 list1 = [4, 5, 6] a. [1, 2, 3] b. [4, 5, 6] c. [1, 2, 3, 4, 5, 6] d. invalid code 10. What would be the value of the variable list2 after the execution of the following code? 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. invalid code 11. When working with multiple sets of data, one would typically use a(n). a. list b. tuple c. nested list d. Sequence 12. The primary difference between a tuple and list is that. a. when creating a tuple you don t use commas to separate elements 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 13. What is the advantage of using tuples over lists? a. Tuples are not limited in size. b. Tuples can include any data type as an element. c. Processing a tuple is faster than processing a list. d. There is no advantage. 14. What method can be used to convert a list to a tuple? a. append b. tuple c. insert d. list
15. What method can be used to convert a tuple to a list? a. append b. tuple c. insert d. list TRUE/FALSE 1. True/False: Invalid indexes do not cause slicing expressions to raise an exception. 2. True/False: Lists are dynamic data structures such that items may be added to them or removed from them. 3. True/False: Arrays, which most other programming languages allow, have much more capabilities than list structures. 4. True/False: A list cannot be passed as an argument to a function. 5. True/False: The remove method removes all occurrences of the item from a list. 6. True/False: The sort method rearranges the elements of a list so they appear in ascending or descending order. 7. True/False: The first step in calculating the average of the values in a list is to get the total of the values. 8. True/False: Indexing starts at 1, so the index of the first element is 1, the index of the second element is 2, and so forth. 9. True/False: The index 1 identifies the last element in a list. 10. True/False: In slicing, if the end index specifies a position beyond the end of the list, Python will use the length of the list instead. FILL IN THE BLANK 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. 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 the list. 10. The function returns the item that has the lowest value in the sequence. Chapter Nine MULTIPLE CHOICE 1. What are the valid indexes for the string New York? a. 0 through 7 b. 0 through 8 c. -1 through -8 d. -1 through 6 2. What will be assigned to s_string after the execution of the following code? special = 1357 Country Ln. s_string = special[ :4] a. 7 b. 1357 c. 1357 d. Invalid code 3. What will be assigned to s_string after the execution of the following code? special = 1357 Country Ln. s_string = special[4: ] a. 1357 b. Country Ln. c. Country Ln. d. Invalid code 4. What will be assigned to the string variable even after the execution of the following code? special = 0123456789 even = special[0:10:2] a. 13579 b. 2468 c. 02468 d. Invalid code 5. What will be assigned to s_string after the execution of the following code? special = 1357 Country Ln. s_string = special[-3: ] a. 531 b. Ln.
c. Ln. d. 7531 6. If the start index is the end index, the slicing expression will return an empty string. a. equal to b. less than c. greater than d. not equal to 7. Which of the following string methods can be used to determine if a string contains only \n characters? a. ischar() b. isalpha() c. istab() d. isspace() 8. What is the return value of the string method lstrip()? a. The string with all whitespace removed b. The string with all leading spaces removed c. The string with all leading tabs removed d. The string with all leading whitespaces removed 9. What will be assigned to the string variable pattern after the execution of the following code? i = 3 pattern = z * (5*i) a. zzzzzzzzzzzzzzz b. zzzzz c. Error: * operator used incorrectly d. The right side of the * must be an integer. 10. Which list will be referenced by the variable list_strip after the execution of the following code? list_string = 03/07/2008 list_strip = list_string.split( / ) a. [ 3, 7, 2008 ] b. [ 03, 07, 2008 ] c. [ 3, /, 7, /, 2008 ] d. [ 03, /, 07, /, 2008 ] 11. What is the first negative index in a string? a. 0 b. -1 c. -0 d. Size of the string minus one 12. Which method would you use to determine whether a substring is present in a string? a. endswith(substring) b. find(substring) c. replace(string, substring)
d. startswith(substring) 13. Which method would you use to determine whether a substring is the suffix of a string? a. endswith(substring) b. find(substring) c. replace(string, substring) d. startswith(substring) 14. What is the value of the variable string after the execution of the following code? string = 'abcd' string.upper() a. 'abcd' b. 'Abcd' c. 'ABCD' d. Invalid code 15. What is the value of the variable string after the execution of the following code? string = 'Hello' string += ' world' a. 'Hello' b. ' world' c. 'Hello world' d. Invalid code TRUE/FALSE 1. True/False: Invalid indexes do not cause slicing expressions to raise an exception. 2. True/False: Indexing works with both strings and lists. 3. True/False: Indexing of a string starts at 1, so the index of the first character is 1, the index of the second character is 2, and so forth. 4. True/False: The index 1 identifies the last character in a string. 5. True/False: In slicing, if the end index specifies a position beyond the end of the string, Python will use the length of the string instead. 6. True/False: An expression of the form string[i] = 'i' is a valid expression. 7. True/False: If the + operator is used on strings, it produces a string that is the combination of the two strings used as its operands. 8. True/False: When accessing each character in a string, such as for copying purposes, you would typically use a while loop. 9. True/False: If a whole paragraph is included in a single string, the split() method can be used to obtain a list of the sentences included in the paragraph.
10. True/False: The strip() method returns a copy of the string with all leading whitespace characters removed, but does not remove trailing whitespace characters. FILL IN THE BLANK 1. Each character in a string has a(n) which specifies its position in the string. 2. A(n) exception will occur if you try to use an index that is out of range for a particular string. 3. Strings are, which means that once a string is created, it cannot be changed. 4. A(n) is a span of characters that are taken from within a string. 5. The operator can be used to determine whether one string is contained in another string. 6. The method returns true if the string contains only numeric digits. 7. When the operand on the left side of the * symbol is a string and the operand on the right side is an integer, it becomes the operator. 8. The method returns the list of the words in the string. 9. The method returns a copy of the string with all alphabetic letters converted to lower case. 10. The third number in string slicing brackets represents the value.