Files and exceptions Flashcards

1
Q

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
A

try to find the function first within our program

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Python allows the programmer to work with text and number files.

TRUE
FALSE

A

FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The ZeroDivisionError exception is raised when the program attempts to perform the calculation x/y if y = 0.

TRUE
FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

If the last line in a file is not terminated with \n, the readline method will return the line without \n.

TRUE
FALSE

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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

A

TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

The data is copied from a variable in RAM to a file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

random

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Which mode specifier will open a file but not let you change the file or write to it?

‘r’
‘a’
‘e’
‘w’

A

‘r’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which method could be used to strip specific characters from the end of a string?

strip
rstrip
estrip
remove

A

rstrip

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

a try/except statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

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')
A

customer.write(‘Mary Smith’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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
A

read

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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')
A

outfile = open(‘users.txt’, ‘w’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

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.
A

ERROR: cannot have 0 items

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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
A

Nothing; there is no print statement to display average. The ValueError will not catch the error.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly