Test Questions & Answers Chapter 6 Files And Exceptions - Test Bank | Python 5e by Gaddis by Tony Gaddis. DOCX document preview.

Test Questions & Answers Chapter 6 Files And Exceptions

Starting Out with Python 5e (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.

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

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

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

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

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

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

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

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.

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

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

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

a.

retrieving data

b.

reading data

c.

reading input

d.

getting data

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.

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

5. How many types of files are there?

a.

one

b.

two

c.

three

d.

more than three

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

a.

sequential

b.

random

c.

numbered

d.

text

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

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

a.

variable

b.

delimiter

c.

field

d.

data bit

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'

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'

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

a.

estrip

b.

rstrip

c.

strip

d.

remove

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

a.

str

b.

value

c.

num

d.

chr

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

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

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

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

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)

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

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

if __name__ == '__main__':

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.

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

if __name__ == '__main__':

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.

COMPLETION

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

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

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

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

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

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

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

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

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

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

Document Information

Document Type:
DOCX
Chapter Number:
6
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 6 Files And Exceptions
Author:
Tony Gaddis

Connected Book

Test Bank | Python 5e by Gaddis

By Tony Gaddis

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party