CS Exam#2 ch.7
What is the result of the variable position after the following code snippet is executed? inFile.seek(0) inFile.seek(8, SEEK_CUR) inFile.seek(-3, SEEK_CUR) position = inFile.tell()
5
Which of the following methods strips specified punctuation from the front or end of each string (s)?
s.strip(".!?;:")
What is the name of the file object variable in the following code segment? a = open("b.txt", "r")
a
What method(s) can be used to write to a file?
write, print
In the following code snippet, what happens if the "output.txt" file does not exist? outfile = open("output.txt", "w")
An empty file is created
What portable file format is commonly used to export data from a spreadsheet so that it can be read and processed by a Python program?
Comma-Separated Values (CSV)
What exception is raised by the following code segment? data = ["A", "B", "C", "D"] print(data[4])
IndexError
In the code snippet below, if the file contains the following words: Monday! Tuesday. Wednesday? stored one per line, what would be the output? infile = open("input.txt", "r") for word in infile : word = word.rstrip(".!\n") print(word)
Monday Tuesday Wednesday?
What happens when the following code segment executes if test.txt does not exist? outfile = open("test.txt", "w")
The file test.txt is created as a new empty file
The following code segment is supposed to read and display the contents of a file. What problem does it have in its current form? infile = open("test.txt", "r") line = infile.readline() while line != "" : print(line) line = infile.readline() infile.close()
The program displays the contents of the file, but it is double spaced
Which character encoding standard uses sequences of between 1 and 4 bytes to represent a huge number of different characters?
UTF-8
Given the following command line, where are the arguments stored? python program.py -v input.dat
argv list
If a program is invoked with python program.py -r input.dat output.dat, what are the elements of argv?
argv[0]: "program.py" argv[1]: "-r" argv[2]: "input.dat" argv[3]: "output.dat"
Consider the following code segment: try : inputFile = open("lyrics.txt", "r") line = inputFile.readline() print(line) _____________________ print("Error") What should be placed in the blank so that the program will print Error instead of crashing if an exception occurs while opening or reading from the file?
except IOError :
Consider a program that wants to read a file from the absolute path c:\users\user1\states.dat. What statement allows you to read this file?
file = open("c:\\users\\user1\\states.dat", "r")
Which statement imports all of the functions in Python's regular expression library?
from re import *
hich statement must appear in a program before the command line arguments can be accessed?
from sys import argv
Which of the following statements opens a text file for reading?
infile = open("myfile.txt", "r")
Which statement is used to close the file object opened with the following statement? infile = open("test.txt", "r")
infile.close()
Which of the following methods provides a way to split the contents of the string sentence into a list of individual words?
sentence.split()