Completed
Text Files - read/write/seek
Questions From This Challenge
Q1mcq1 mark
What does f.tell() return for an open file object f?
The total number of lines in the file
The current position of the file pointer from the start of the file ✓
The name of the file
The mode in which the file was opened
Q2mcq1 mark
Which method writes a list of strings to a file in one call, without automatically adding any newline characters between them?
write()
writelines() ✓
writerow()
print()
Q3mcq1 mark
What is the key difference between read() and readline()?
read() reads the entire file content as one string; readline() reads one line at a time ✓
read() reads only the first line; readline() reads the whole file
Both behave identically in every situation
read() only works on files opened in binary mode
Q4mcq1 mark
If f.seek(0) is called on an open file, what happens?
The file pointer moves to the end of the file
The file pointer moves to the beginning of the file ✓
The file is closed immediately
All content in the file is deleted
Q5mcq1 mark
What does readlines() return when called on an open text file object?
A single string containing the entire file content
A list of strings, one for each line, with newline characters retained ✓
A dictionary mapping line numbers to text
An integer giving the total number of lines
Q6mcq1 mark
A text file "notes.txt" contains "Hi\nBye". What does f.readline() return the FIRST time it is called, immediately after opening the file in "r" mode?
"Hi\n" ✓
"Bye"
"Hi\nBye"
An empty string ""
Q7output1 mark
What is the output?
with open("t1.txt", "w") as f:
f.write("Python\nProgramming")
with open("t1.txt", "r") as f:
print(f.readline().strip())
Correct output:
PythonQ8output1 mark
What is the output?
with open("t2.txt", "w") as f:
f.writelines(["Line1\n", "Line2\n", "Line3\n"])
with open("t2.txt", "r") as f:
lines = f.readlines()
print(lines)
Correct output:
['Line1\n', 'Line2\n', 'Line3\n']Q9output1 mark
What is the output?
with open("t3.txt", "w") as f:
f.write("ABCDEFGH")
with open("t3.txt", "r") as f:
f.seek(3)
print(f.tell())
print(f.read(2))
Correct output:
3
DEQ10output1 mark
What is the output?
with open("t4.txt", "w") as f:
f.writelines(["Cat", "Dog", "Fish"])
with open("t4.txt", "r") as f:
print(f.read())
Correct output:
CatDogFishQ11output1 mark
What is the output?
with open("t5.txt", "w") as f:
f.write("One\nTwo\nThree\n")
with open("t5.txt", "r") as f:
for line in f:
print(line.strip())
Correct output:
One
Two
ThreeQ12output1 mark
What is the output?
with open("t6.txt", "w+") as f:
f.write("Hello Seek")
f.seek(0)
print(f.read())
Correct output:
Hello SeekQ13output1 mark
What is the output?
with open("t7.txt", "w") as f:
f.write("12345")
print(f.tell())
Correct output:
5Q14output1 mark
What is the output?
with open("t8.txt", "w") as f:
f.write("Alpha\nBeta\nGamma")
with open("t8.txt", "r") as f:
print(f.readline().strip())
print(f.readline().strip())
Correct output:
Alpha
BetaQ15output1 mark
What is the output?
def countLines(filename):
with open(filename, "r") as f:
data = f.readlines()
return len(data)
with open("t9.txt", "w") as f:
f.writelines(["A\n", "B\n", "C\n", "D\n"])
print(countLines("t9.txt"))
Correct output:
4