Chapter 05 – An Introduction to Programming using Python Schneider
Download file with the answers
Chapter 05 - An Introduction to Programming using Python Schneider
1 file(s) 116.97 KB
If you are not a member register here to download this file
Chapter 5
1. When reading data from a file, the open function returns a(n) __________.
a. file object
b. file name
c. file handle
d. file tuple
2. What function do you use to terminate a connection to a file?
a. close
b. terminate
c. stop
d. disconnect
3. After all the lines of a file have been read, the readline method returns __________.
a. the empty string
b. an empty tuple
c. the value None
d. a Throwback error
4. Python uses a(n) __________ as a temporary holding place for data to be written to disk.
a. buffer
b. temp space
c. special memory location
d. list
5. When are the contents of the buffer written to disk?
a. When the buffer is full.
b. When the file is closed.
c. Both a & b.
d. None of the above.
6. Which standard library module do you need to import in order to use the remove and rename functions for files?
a. os
b. file
c. path
d. pickle
7. A(n) __________ is an unordered collection of items with no duplicates.
a. set
b. file
c. dictionary
d. tuple
8. Elements of a set are delimited with __________.
a. { }
b. [ ]
c. ( )
d. < >
9. The statement set1.union(set2) is:
a. the set containing the elements that are in either set1 and set2 without duplicates
b. the set containing the elements that are in both set1 and set2
c. the set containing the elements that are in set1 with the elements of set2 removed
d. the set containing the elements that are in set2 with the elements of in set1 removed
10. The statement set1.intersection(set2) is:
a. the set containing the elements that are in both set1 and set2
b. the set containing the elements that are in either set1 and set2 without duplicates
c. the set containing the elements that are in set1 with the elements of set2 removed
d. the set containing the elements that are in set2 with the elements of in set1 removed
11. The statement set1.difference(set2) is:
a. the set containing the elements that are in set1 with the elements of set2 removed
b. the set containing the elements that are in set2 with the elements of in set1 removed
c. the set containing the elements that are in both set1 and set2
d. the set containing the elements that are in either set1 and set2 without duplicates
12. An attempt to open a nonexistent file for input:
a. generates a runtime error
b. generates a syntax error
c. creates an empty input file
d. none of the above
13. If a file that already exists is opened for writing:
a. the contents of the file will be erased
b. the new data to be written will be appended to the end of the rile
c. a Throwback error will occur
d. the user will be prompted for the action they wish to take
14. The default mode for opening a file is
a. reading
b. writing
c. appending
d. deleting
15. To avoid a potential runtime error when opening files for reading or writing:
a. use the os.path.isfile function
b. use the os.path.file.exists function
c. prompt the user for the action to take if the file does not exist
d. use the Boolean value try to check if the file exists
16. What is the output of the following Python statement?
print (set(“bookkeeper”))
a. {‘b’, ‘o’, ‘k’, ‘e’, ‘p’, ‘r’}
b. {‘b’, ‘o’, ‘o’, ‘k’, ‘k’, ‘e’, ‘e’, ‘p’, ‘e’, ‘r’}
c. {‘o’, ‘k’, ‘e’}
d. {‘b’, ‘p’, ‘r’}
17. Each line of a CSV file is referred to as a(n) __________.
a. record
b. tuple
c. field
d. comma field
18. Each piece of data in a CSV file record is referred to as a(n) __________.
a. field
b. record
c. tuple
d. line
19. In a dictionary, a pair such such as “dog” : “rover” is called a(n) __________.
a. item
b. pair
c. key
d. couple
20. Which file format stores data as a sequence of types that can only be access by special readers?
a. binary
b. text
c. CSV-formatted
d. all of the above
21. In order for Python to use functions to work with binary files, you must first import which standard library module?
a. pickle
b. os
c. binaries
d. osfile
True/False (23)
1. After all the lines of a file have been read, the readline method returns the value None.
Answer: false
2. You must close a file in order to guarantee that all data has been physically written to the disk.
Answer: true
3. The remove and rename functions cannot be used with open files.
Answer: true
4. Sets cannot contain lists.
Answer: true
5. Sets can contain other sets.
Answer: false
6. Elements of a set have no order.
Answer: true
7. Elements of a set may be duplicated.
Answer: false
8. Two sets are equal if they contain the same elements.
Answer: true
9. Elements if a set cannot be ordered.
Answer: true
10. Sets cannot be created with comprehension.
Answer: false
11. infile is a descriptive name bot not mandatory for file input usage.
Answer: true
12. An attempt to open a nonexistent file for input generates a syntax error.
Answer: false
13. If a file that already exists is opened for writing, the contents of the file will be erased.
Answer: true
14. The default mode for opening a file is writing.
Answer: false
15. Only strings can be written to text file.
Answer: true
16. The value of set() is the empty set.
Answer: true
17. The data in the fields of each record in a CSV file normally should be related.
Answer: true
18. In a dictionary, keys must be immutable objects.
Answer: true
19. It is common to create dictionaries from text files.
Answer: true
20. Dictionaries cannot have other dictionaries as values.
Answer: false
21. A dictionary is an ordered structure that can be sorted.
Answer: false
22. Dictionaries cannot be created with comprehension.
Answer: false
23. Dictionary comprehension can be used to extract a subset of a dictionary.
Answer: true
Short Answer (11)
1. Complete the following function to open the file for reading and read the contents into a single string named contents.
def readFile(file):
Answer:
infile = open(file, ‘r’)
contents = infile.read()
2. Write a Python statement to open a file called names for writing and assign it to a variable called outfile.
Answer: outfile = open(names, ‘w’)
3. Write a Python statement to open a file called grades with the intent to add values to the end of the file and assign it to a variable called outfile.
Answer: outfile = open(grades, ‘a’)
4. Write a single Python statement to convert the list [“spring”, “summer”, “fall”, “winter”] to a set called seasons.
Answer: seasons = set([“spring”, “summer”, “fall”, “winter”])
5. Write a single Python statement to convert the tuple (“spring”, “summer”, “fall”, “winter”) to a set called seasons.
Answer: seasons = set((“spring”, “summer”, “fall”, “winter”))
6. Why can’t elements of a set be indexed?
Answer: Elements of a set cannot be indexed have no order.
7. Explain the difference between a simple text file and a CSV-formatted file.
Answer: A simple text file has a single piece of data per line. A CSV-formatter file has several items of data on each line with items separated by commas.
8. Write a Python statement to create an empty dictionary called dogs.
Answer: dogs = { }
9. Write a Python statement to create a copy of the dictionary called dogs into a new dictionary called canines.
Answer: canines = dict(dogs)
10. Create a dictionary called dogs for the following data.
Eddie Jack Russell
Lassie Collie
Ping Beagle
Answer: dogs = {“Eddie” : “Jack Russell”, “Lassie” : “Collie”, “Ping” : “Beagle”}
11. Why can’t lists and sets serve as keys for dictionaries?
Answer: Because dictionary keys must be immutable objects. Lists and sets are mutable.
Leave a reply