Gaddis Python 2e Chapter 07
Download file with the answers
Gaddis Python 2e Chapter 07
1 file(s) 168.73 KB
If you are not a member register here to download this file
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.
ANS: F
2. True/False: When a piece of data is read from a file, it is copied from the file into the program.
ANS: F
3. True/False: Closing a file disconnects the communication between the file and the program.
ANS: T
4. True/False: Python allows the programmer to work with text and number files.
ANS: F
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.
ANS: F
6. True/False: The ZeroDivisionError exception is raised when the program attempts to perform a division by zero.
ANS: T
7. True/False: An exception handler is a piece of code that is written using the try/except statement.
ANS: T
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.
ANS: T
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.
ANS: T
10. True/False: It is possible to create a while loop that determines when the end of a file has been reached.
ANS: T
FILL IN THE BLANK
1. When a program needs to save data for later use, it writes the data in a(n) _______________.
ANS: file
2. Programmers usually refer to the process of _______________ data in a file as “writing data to” the file.
ANS: saving
3. The term _______________ file is used to describe a file to which data is written.
ANS: output
4. The term _______________ file is used to describe a file from which the program gets data.
ANS: input
5. A(n) _______________ file contains data that has been encoded as text, using a scheme such as ASCII.
ANS: text
6. A(n) _______________ file contains data that has not been converted to text.
ANS: binary
7. A(n) _______________ access file retrieves data from the beginning of the file to the end of the file.
ANS: sequential
8. A filename _______________ is a short sequence of characters that appear at the end of a filename preceded by a period.
ANS: extension
9. A(n) _______________ gives information regarding the line number(s) that caused an exception.
ANS: traceback
10. A(n) _______________ includes one or more statements that can potentially raise an exception.
ANS: try block
Leave a reply