Week 6 Flashcards
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.
False
When a piece of data is read from a file, it is copied from the file into the program.
False
Closing a file disconnects the communication between the file and the program.
True
In Python, there is nothing that can be done if the program tries to access a file to read that does not exist.
False
Python allows the programmer to work with text and number files.
False
The ZeroDivisionError exception is raised when the program attempts to perform the calculation x/y if y = 0.
True
An exception handler is a piece of code that is written using the try/except statement.
True
If the last line in a file is not terminated with \n, the readline method will return the line without \n.
True
Strings can be written directly to a file with the write method, but numbers must be converted to strings before they can be written.
True
It is possible to create a while loop that determines when the end of a file has been reached.
True
Which of the following is associated with a specific file and provides a way for the program to work with that file?
a. the filename
b. the file extension
c. the file object
d. the file variable
c. the file object
What is the process of retrieving data from a file called?
a. retrieving data
b. reading data
c. reading input
d. getting data
b. reading data
Which of the following describes what happens when a piece of data is written to a file?
a. The data is copied from a variable in RAM to a file.
b. The data is copied from a variable in the program to a file.
c. The data is copied from the program to a file.
d. The data is copied from a file object to a file.
a. The data is copied from a variable in RAM to a file.
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
a. open the file
How many types of files are there?
a. one
b. two
c. three
d. more than three
b. two
A(n) __________ access file is also known as a direct access file.
a. sequential
b. random
c. numbered
d. text
b. random
Which type of file access jumps directly to a piece of data in the file without having to read all the data that comes before it?
a. sequential
b. random
c. numbered
d. text
b. random
A single piece of data within a record is called a
a. variable
b. delimiter
c. field
d. data bit
c. field
Which mode specifier will erase the contents of a file if it already exists and create the file if it does not already exist?
a. ‘w’
b. ‘r’
c. ‘a’
d. ‘e’
a. ‘w’
Which mode specifier will open a file but not let you change the file or write to it?
a. ‘w’
b. ‘r’
c. ‘a’
d. ‘e’
b. ‘r’
Which method could be used to strip specific characters from the end of a string?
a. estrip
b. rstrip
c. strip
d. remove
b. rstrip
Which method could be used to convert a numeric value to a string?
a. str
b. value
c. num
d. chr
a. str
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
d. readline
Which statement can be used to handle some of the runtime errors in a program?
a. an exception statement
b. a try statement
c. a try/except statement
d. an exception handler statement
c. a try/except statement
Given 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’)
d. customer.write(‘Mary Smith’)
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
d. read
Which of the following is the correct way to open a file named users.txt in ‘r’ mode?
a. infile = open(‘r’, users.txt)
b. infile = read(‘users.txt’, ‘r’)
c. infile = open(‘users.txt’, ‘r’)
d. infile = readlines(‘users.txt’, r)
c. infile = open(‘users.txt’, ‘r’)
Which of the following is the correct way to open a file named users.txt to write to it?
a. outfile = open(‘w’, users.txt)
b. outfile = write(‘users.txt’, ‘w’)
c. outfile = open(‘users.txt’, ‘w’)
d. outfile = open(‘users.txt’)
c. outfile = open(‘users.txt’, ‘w’)
What will be the output after the following code is executed and the user enters 75 and 0 at the first two prompts?
def main():
try:
total = int(input(“Enter total cost of items? “))
num_items = int(input(“Number of items “))
average = total / num_items
except ZeroDivisionError:
print(‘ERROR: cannot have 0 items’)
except ValueError:
print(‘ERROR: number of items cannot be negative’)
main()
ERROR: cannot have 0 items
What will be the output after the following code is executed and the user enters 75 and -5 at the first two prompts?
def main():
try:
total = int(input(“Enter total cost of items? “))
num_items = int(input(“Number of items “))
average = total / num_items
except ZeroDivisionError:
print(‘ERROR: cannot have 0 items’)
except ValueError:
print(‘ERROR: number of items cannot be negative’)
main()
Nothing, there is no print statement to display average. The ValueError will not catch the error.
When a program needs to save data for later use, it writes the data in a(n) ___________.
file
Programmers usually refer to the process of __________ data in a file as writing data to the file.
saving
When data is written to a file, it is described as a(n) __________ file.
output
If data is retrieved from a file by a program, this is known by the term __________ file.
input
A(n) ___________ file contains data that has been encoded as text, using a scheme such as ASCII.
text
A(n) ___________ access file retrieves data from the beginning of the file to the end of the file.
sequential
A(n) __________ file contains data that has not been converted to text.
binary
A filename __________ is a short sequence of characters that appear at the end of a filename, preceded by a period.
extension
A(n) __________ gives information about the line number(s) that caused an exception.
traceback
A(n) ___________ block includes one or more statements that can potentially raise an exception.