Peer Instruction: Text Files Multiple Choice QuestionsΒΆ
- open
- Incorrect! Open will be called to open the file to be read.
- A loop or method for reading (e.g. read)
- Incorrect! A loop or method for reading is required to read an open file.
- write
- Incorrect! Write will be called to write text on to another file.
- close
- Incorrect! Close will be called to after the program is done reading or writing a file.
- All should be called
- Correct! All these functions/methods will be called by the program.
Q-1: A program is designed to retrieve some data from a file, process it, and output the revised data to another file. Which of the following functions/methods will not be called in the program?
- The first line
- Incorrect! Because readline() has been called five times. So skip will return the fifth line.
- The second line
- Incorrect! Because readline() has been called five times. So skip will return the fifth line.
- The third line
- Incorrect! Because readline() has been called five times. So skip will return the fifth line.
- The fourth line
- Incorrect! Because readline() has been called five times. So skip will return the fifth line.
- The fifth line
- Correct! Because readline() has been called five times. So skip will return the fifth line.
Q-2: Here are the first few lines from a file- songs.txt:
Songs Chosen this Semester #Game names for the songs #All ridiculously good soundtracks Sep, 17, Chrono Cross Sep, 19, Zelda 3
f refers to this file and has just been opened. What does skip return?
def skip(f):
line = f.readline()
line = f.readline()
while line.startswith('#' ):
line = f.readline()
return f.readline()
- The first line
- Incorrect! Because line was last assigned to the fourth iteration of readline().
- The second line
- Incorrect! Because line was last assigned to the fourth iteration of readline().
- The third line
- Incorrect! Because line was last assigned to the fourth iteration of readline().
- The fourth line
- Correct! Because line was last assigned to the fourth iteration of readline().
- The fifth line
- Incorrect! Because line was last assigned to the fourth iteration of readline().
Q-3: Here are the first few lines from a file- songs.txt:
Songs Chosen this Semester #Game names for the songs #All ridiculously good soundtracks Sep, 17, Chrono Cross Sep, 19, Zelda 3
f refers to this file and has just been opened. What does skip return?
def skip(f):
line = f.readline()
line = f.readline()
while line.startswith('#' ):
line = f.readline()
return line
You have attempted of activities on this page