TEST
TEST
TEST
1. Write a method DISPLAYWORDS() in python to read lines from a text file STORY.TXT and display
those words ,which are less than 4 characters.
2. Write statements to place the file fp1:
i) To beginning of the file
ii) To 25th byte from the beginning
iii) To 10 bytes behind the current position of the file pointer.
iv) To 10 bytes behind the EOF position
3. What this following code do –
F=open(‘poem.txt’,’r’)
f.read(100)
4. File stu.dat stores the record-
{'rollno’:11,’name’:’sia’,’marks’:85.5}
{'rollno’:12,’name’:’ria’,’marks’:65.5}
{'rollno’:13,’name’:’jia’,’marks’:75.5}
Write a program to modify the rollno 12 as ‘riya’ in file “File.stu’.
5. Write a program to count the words ’to’ and ‘the’ present in a text file ‘poem.txt’.
6. MCQ’s
1. To open a file c:\scores.txt for reading, 4. Which of the following statements are
we use _____________ true?
a) infile = open(“c:\scores.txt”, “r”) a) When you open a file for reading, if the
b) infile = open(“c:\\scores.txt”, “r”) file does not exist, an error occurs
c) infile = open(file = “c:\scores.txt”, “r”) b) When you open a file for writing, if the
d) infile = open(file = “c:\\scores.txt”, “r”) file does not exist, a new file is created
c) When you open a file for writing, if the
2. To open a file c:\scores.txt for writing, we file exists, the existing file is overwritten
use ____________ with the new file
a) outfile = open(“c:\scores.txt”, “w”) d) All of the mentioned
b) outfile = open(“c:\\scores.txt”, “w”)
c) outfile = open(file = “c:\scores.txt”, “w”) 5. To read two characters from a file object
d) outfile = open(file = “c:\\scores.txt”, “w”) infile, we use ____________
a) infile.read(2)
3. To open a file c:\scores.txt for appending b) infile.read()
data, we use ____________ c) infile.readline()
a) outfile = open(“c:\\scores.txt”, “a”) d) infile.readlines()
b) outfile = open(“c:\\scores.txt”, “rw”)
c) outfile = open(file = “c:\scores.txt”, “w”) 6. To read the entire remaining contents of
d) outfile = open(file = “c:\\scores.txt”, “w”) the file as a string from a file object infile,
we use ____________
a) infile.read(2)
b) infile.read() a) sets the file’s current position at the
c) infile.readline() offset
d) infile.readlines() b) sets the file’s previous position at the
offset
7. What will be the output of the following c) sets the file’s current position within the
Python code? file
f = None d) none of the mentioned
for i in range (5):
with open("data.txt", "w") as f: 12. What is the use of tell() method in
if i > 2: python?
break a) tells you the current position within the
print(f.closed) file
a) True b) tells you the end position within the file
b) False c) tells you the file is opened or not
c) None d) none of the mentioned
d) Error
13. Correct syntax of file.writelines() is?
8. To read the next line of the file from a file a) file.writelines(sequence)
object infile, we use ____________ b) fileObject.writelines()
a) infile.read(2) c) fileObject.writelines(sequence)
b) infile.read() d) none of the mentioned
c) infile.readline()
d) infile.readlines() 14. Correct syntax of file.readlines() is?
a) fileObject.readlines( sizehint );
9. To read the remaining lines of the file b) fileObject.readlines();
from a file object infile, we use c) fileObject.readlines(sequence)
____________ d) none of the mentioned
a) infile.read(2)
b) infile.read() 15. What is the difference between r+ and
c) infile.readline() w+ modes?
d) infile.readlines() a) no difference
b) in r+ the pointer is initially placed at the
10. The readlines() method returns beginning of the file and the pointer is at
____________ the end for w+
a) str c) in w+ the pointer is initially placed at the
b) a list of lines beginning of the file and the pointer is at
c) a list of single characters the end for r+
d) a list of integers d) depends on the operating system
16.
11. What is the use of seek() method in
files?