Files and exceptions Flashcards
If we don’t specify the module name before calling a module function, the Python interpreter will ________.
try to find the function first within our program immediately fail ignore the function call prompt the user to enter the module name
try to find the function first within our program
Python allows the programmer to work with text and number files.
TRUE
FALSE
FALSE
The ZeroDivisionError exception is raised when the program attempts to perform the calculation x/y if y = 0.
TRUE
FALSE
TRUE
If the last line in a file is not terminated with \n, the readline method will return the line without \n.
TRUE
FALSE
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
FALSE
TRUE
Which of the following describes what happens when a piece of data is written to a file?
The data is copied from a variable in the program to a file.
The data is copied from the program to a file.
The data is copied from a file object to a file.
The data is copied from a variable in RAM to a file.
The data is copied from a variable in RAM to a file.
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?
numbered
text
sequential
random
random
Which mode specifier will open a file but not let you change the file or write to it?
‘r’
‘a’
‘e’
‘w’
‘r’
Which method could be used to strip specific characters from the end of a string?
strip
rstrip
estrip
remove
rstrip
Which statement can be used to handle some of the runtime errors in a program?
an exception handler statement a try/except statement a try statement an exception statement
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?
customer file.write('Mary Smith') customer. input('Mary Smith') customer. write('Mary Smith') customer. write('w', 'Mary Smith')
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?
get read write input
read
Which of the following is the correct way to open a file named users.txt to write to it?
outfile = open('users.txt', 'w') outfile = open('users.txt') outfile = open('w', users.txt) outfile = write('users.txt', 'w')
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()
0 ERROR: cannot have 0 items ERROR: number of items can't be negative Nothing; there is no print statement to display average.
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()
ERROR: cannot have 0 items ERROR: number of items can't be negative Nothing; there is no print statement to display average. The ValueError will not catch the error. ERROR: number of items can't be negative ERROR: cannot have 0 items
Nothing; there is no print statement to display average. The ValueError will not catch the error.