Deck 6: Files and Exceptions

Full screen (f)
exit full mode
Question
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.
Use Space or
up arrow
down arrow
to flip the card.
Question
In Python,there is nothing that can be done if the program tries to access a file to read that does not exist.
Question
The ZeroDivisionError exception is raised when the program attempts to perform the calculation x/y if y = 0.
Question
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'
Question
A single piece of data within a record is called a

A) variable
B) delimiter
C) field
D) data bit
Question
An exception handler is a piece of code that is written using the try/except statement.
Question
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'
Question
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
Question
How many types of files are there?

A) one
B) two
C) three
D) more than three
Question
Closing a file disconnects the communication between the file and the program.
Question
A(n)__________ access file is also known as a direct access file.

A) sequential
B) random
C) numbered
D) text
Question
If the last line in a file is not terminated with \n,the readline method will return the line without \n.
Question
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
Question
Python allows the programmer to work with text and number files.
Question
Strings can be written directly to a file with the write method,but numbers must be converted to strings before they can be written.
Question
When a piece of data is read from a file,it is copied from the file into the program.
Question
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
Question
What is the process of retrieving data from a file called?

A) retrieving data
B) reading data
C) reading input
D) getting data
Question
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.
Question
It is possible to create a while loop that determines when the end of a file has been reached.
Question
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
Question
What will be the output after the following code is executed and the user enters 75 and -5 at the first two prompts?
<strong>What will be the output after the following code is executed and the user enters 75 and -5 at the first two prompts?  </strong> A) ERROR: cannot have 0 items B) ERROR: cannot have 0 items ERROR: number of items can't be negative C) ERROR: number of items can't be negative D) Nothing; there is no print statement to display average. The ValueError will not catch the error. <div style=padding-top: 35px>

A) ERROR: cannot have 0 items
B) ERROR: cannot have 0 items
ERROR: number of items can't be negative
C) ERROR: number of items can't be negative
D) Nothing; there is no print statement to display average. The ValueError will not catch the error.
Question
If data is retrieved from a file by a program,this is known by the term __________ file.
Question
When a program needs to save data for later use,it writes the data in a(n)___________.
Question
Which method could be used to strip specific characters from the end of a string?

A) estrip
B) rstrip
C) strip
D) remove
Question
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)
Question
When data is written to a file,it is described as a(n)__________ file.
Question
A(n)__________ gives information about the line number(s)that caused an exception.
Question
Programmers usually refer to the process of __________ data in a file as writing data to the file.
Question
A(n)___________ block includes one or more statements that can potentially raise an exception.
Question
A(n)__________ file contains data that has not been converted to text.
Question
A filename __________ is a short sequence of characters that appear at the end of a filename,preceded by a period.
Question
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')
Question
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
Question
What will be the output after the following code is executed and the user enters 75 and 0 at the first two prompts?
<strong>What will be the output after the following code is executed and the user enters 75 and 0 at the first two prompts?  </strong> A) ERROR: cannot have 0 items B) ERROR: number of items can't be negative C) 0 D) Nothing; there is no print statement to display average. <div style=padding-top: 35px>

A) ERROR: cannot have 0 items
B) ERROR: number of items can't be negative
C) 0
D) Nothing; there is no print statement to display average.
Question
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')
Question
A(n)___________ file contains data that has been encoded as text,using a scheme such as ASCII.
Question
A(n)___________ access file retrieves data from the beginning of the file to the end of the file.
Question
Which method could be used to convert a numeric value to a string?

A) str
B) value
C) num
D) chr
Question
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
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 6: Files and Exceptions
1
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
2
In Python,there is nothing that can be done if the program tries to access a file to read that does not exist.
False
3
The ZeroDivisionError exception is raised when the program attempts to perform the calculation x/y if y = 0.
True
4
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'
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
A single piece of data within a record is called a

A) variable
B) delimiter
C) field
D) data bit
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
An exception handler is a piece of code that is written using the try/except statement.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
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'
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
How many types of files are there?

A) one
B) two
C) three
D) more than three
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
Closing a file disconnects the communication between the file and the program.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
A(n)__________ access file is also known as a direct access file.

A) sequential
B) random
C) numbered
D) text
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
If the last line in a file is not terminated with \n,the readline method will return the line without \n.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
Python allows the programmer to work with text and number files.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
Strings can be written directly to a file with the write method,but numbers must be converted to strings before they can be written.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
When a piece of data is read from a file,it is copied from the file into the program.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
What is the process of retrieving data from a file called?

A) retrieving data
B) reading data
C) reading input
D) getting data
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
It is possible to create a while loop that determines when the end of a file has been reached.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
What will be the output after the following code is executed and the user enters 75 and -5 at the first two prompts?
<strong>What will be the output after the following code is executed and the user enters 75 and -5 at the first two prompts?  </strong> A) ERROR: cannot have 0 items B) ERROR: cannot have 0 items ERROR: number of items can't be negative C) ERROR: number of items can't be negative D) Nothing; there is no print statement to display average. The ValueError will not catch the error.

A) ERROR: cannot have 0 items
B) ERROR: cannot have 0 items
ERROR: number of items can't be negative
C) ERROR: number of items can't be negative
D) Nothing; there is no print statement to display average. The ValueError will not catch the error.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
If data is retrieved from a file by a program,this is known by the term __________ file.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
When a program needs to save data for later use,it writes the data in a(n)___________.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
Which method could be used to strip specific characters from the end of a string?

A) estrip
B) rstrip
C) strip
D) remove
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
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)
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
When data is written to a file,it is described as a(n)__________ file.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
A(n)__________ gives information about the line number(s)that caused an exception.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
Programmers usually refer to the process of __________ data in a file as writing data to the file.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
A(n)___________ block includes one or more statements that can potentially raise an exception.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
A(n)__________ file contains data that has not been converted to text.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
A filename __________ is a short sequence of characters that appear at the end of a filename,preceded by a period.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
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')
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
What will be the output after the following code is executed and the user enters 75 and 0 at the first two prompts?
<strong>What will be the output after the following code is executed and the user enters 75 and 0 at the first two prompts?  </strong> A) ERROR: cannot have 0 items B) ERROR: number of items can't be negative C) 0 D) Nothing; there is no print statement to display average.

A) ERROR: cannot have 0 items
B) ERROR: number of items can't be negative
C) 0
D) Nothing; there is no print statement to display average.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
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')
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
A(n)___________ file contains data that has been encoded as text,using a scheme such as ASCII.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
A(n)___________ access file retrieves data from the beginning of the file to the end of the file.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
Which method could be used to convert a numeric value to a string?

A) str
B) value
C) num
D) chr
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 40 flashcards in this deck.