Week 6 Flashcards

1
Q

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.

A

False

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

When a piece of data is read from a file, it is copied from the file into the program.

A

False

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

Closing a file disconnects the communication between the file and the program.

A

True

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

In Python, there is nothing that can be done if the program tries to access a file to read that does not exist.

A

False

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

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

A

False

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

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

A

True

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

An exception handler is a piece of code that is written using the try/except statement.

A

True

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

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

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
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.

A

True

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

It is possible to create a while loop that determines when the end of a file has been reached.

A

True

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

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

A

c. the file object

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

What is the process of retrieving data from a file called?
a. retrieving data
b. reading data
c. reading input
d. getting data

A

b. reading data

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

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

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
14
Q

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

a. open the file

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

How many types of files are there?
a. one
b. two
c. three
d. more than three

A

b. two

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

A(n) __________ access file is also known as a direct access file.
a. sequential
b. random
c. numbered
d. text

A

b. random

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
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?
a. sequential
b. random
c. numbered
d. text

A

b. random

18
Q

A single piece of data within a record is called a
a. variable
b. delimiter
c. field
d. data bit

A

c. field

19
Q

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

a. ‘w’

20
Q

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’

A

b. ‘r’

21
Q

Which method could be used to strip specific characters from the end of a string?
a. estrip
b. rstrip
c. strip
d. remove

A

b. rstrip

22
Q

Which method could be used to convert a numeric value to a string?
a. str
b. value
c. num
d. chr

A

a. str

23
Q

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

A

d. readline

24
Q

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

A

c. a try/except statement

25
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?
a. customer file.write(‘Mary Smith’)
b. customer.write(‘w’, ‘Mary Smith’)
c. customer.input(‘Mary Smith’)
d. customer.write(‘Mary Smith’)

A

d. customer.write(‘Mary Smith’)

26
Q

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

A

d. read

27
Q

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)

A

c. infile = open(‘users.txt’, ‘r’)

28
Q

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’)

A

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

29
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()

A

ERROR: cannot have 0 items

30
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()

A

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

31
Q

When a program needs to save data for later use, it writes the data in a(n) ___________.

A

file

32
Q

Programmers usually refer to the process of __________ data in a file as writing data to the file.

A

saving

33
Q

When data is written to a file, it is described as a(n) __________ file.

A

output

34
Q

If data is retrieved from a file by a program, this is known by the term __________ file.

A

input

35
Q

A(n) ___________ file contains data that has been encoded as text, using a scheme such as ASCII.

A

text

36
Q

A(n) ___________ access file retrieves data from the beginning of the file to the end of the file.

A

sequential

37
Q

A(n) __________ file contains data that has not been converted to text.

A

binary

38
Q

A filename __________ is a short sequence of characters that appear at the end of a filename, preceded by a period.

A

extension

39
Q

A(n) __________ gives information about the line number(s) that caused an exception.

A

traceback

40
Q

A(n) ___________ block includes one or more statements that can potentially raise an exception.

A