程序代写代做代考 scheme python Starting Out with Python 4e (Gaddis)

Starting Out with Python 4e (Gaddis)
Chapter 6 Files and Exceptions

TRUE/FALSE

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

ANS: F

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

ANS: F

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

ANS: T

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

ANS: F

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

ANS: F

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

ANS: T

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

ANS: T

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

ANS: T

9. Strings can be written directly to a file with the write method, but numbers must be converted to strings before they can be written.

ANS: T

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

ANS: T

MULTIPLE CHOICE

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

ANS: C

2. What is the process of retrieving data from a file called?
a.
retrieving data

b.
reading data

c.
reading input

d.
getting data

ANS: B

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

ANS: A

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

ANS: A

5. How many types of files are there?
a.
one

b.
two

c.
three

d.
more than three

ANS: B

6. A(n) __________ access file is also known as a direct access file.
a.
sequential

b.
random

c.
numbered

d.
text

ANS: B

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

ANS: B

8. A single piece of data within a record is called a
a.
variable

b.
delimiter

c.
field

d.
data bit

ANS: C

9. 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’

ANS: A

10. 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’

ANS: B

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

b.
rstrip

c.
strip

d.
remove

ANS: B

12. Which method could be used to convert a numeric value to a string?
a.
str

b.
value

c.
num

d.
chr

ANS: A

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

ANS: D

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

ANS: C

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

ANS: D

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

ANS: D

17. 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)

ANS: C

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

ANS: C

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

b.
ERROR: number of items can’t be negative

c.
0

d.
Nothing; there is no print statement to display average.

ANS: A

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

ANS: D

COMPLETION

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

ANS: file

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

ANS: saving

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

ANS: output

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

ANS: input

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

ANS: text

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

ANS: sequential

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

ANS: binary

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

ANS: extension

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

ANS: traceback

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

ANS: try block