Must Prepare Python File Handling MCQ Class 12 for Term 1

MCQ on Writing/appending data to a text file using write() and writelines(),

Reading from a text file using read(), readline( ) and readlines()

 

1. The ____________ method returns a list containing each line of the file as a list item.

a) read()

b) readline()

c) readlines() 

d) readone()

c) readlines()

 

 

 

2. If we want to write a sequence of strings,list, tuple into the file then we use _________ function.

a) write()

b) writelines()

c) writerow()

d) append()

b) writelines() 

 

3. read() and readline() functions can be used for reading no of characters from file if size is mentioned.

a) read() and readline() 

b) readlines() and readline()

c) read() and readlines()

d) None of the above

a) read() and readline() 

 

 

4. We can use readline( ) function which can read one line at a time from the file.

a) read()

b) readline() 

c) readlines()

d) readone()

b) readline()

 

 

5. To read twelve characters from a file object fin we use ________

a) fin.read(12) 

b) fin.read()

c) fin.readline()

d) read(12)

a) fin.read(12)

 

6. Python automatically flushes the file buffers before closing a file with close() function.

a) True 

b) False

a) True

 

7. The ________ method in Python file handling clears the internal buffer of the file..

a) close()

b) flush() 

c) clear()

d) open()

b) flush()

 

 

8. Suppose content of ‘Myfile.txt’ is

Humpty Dumpty sat on a wall

Humpty Dumpty had a great fall

All the king’s horses and all the king’s men

Couldn’t put Humpty together again

 

What will be the output of the following code?

myfile = open(“Myfile.txt”)

record = myfile.read().split()

print(len(record))

myfile.close()

a) 24

b) 25

c) 26 

d) 27

c) 26 

 

9. To create a new file or to write on an existing file after truncating / overwriting its old content , file has to be opened in _______ access mode

a) “w+” 

b) “a+”

c) “r+”

d) “wb”

a) “w+” 

 

 

10. Which of the following options can be used to read the first line of a text file Myfile.txt?

a) myfile = open(‘Myfile.txt’); myfile.read()

b) myfile = open(‘Myfile.txt’,’r’); myfile.read(n)

c) myfile = open(‘Myfile.txt’); myfile.readline() 

d) myfile = open(‘Myfile.txt’); myfile.readlines()

c) myfile = open(‘Myfile.txt’); myfile.readline()

 

 

11. Which of the following options can be used to read the whole content of a text file Myfile.txt?

a) myfile = open(‘Myfile.txt’); myfile.read() 

b) myfile = open(‘Myfile.txt’,’r’); myfile.read(n)

c) myfile = open(‘Myfile.txt’); myfile.readline()

d) myfile = open(‘Myfile.txt’); myfile.readlines()

a) myfile = open(‘Myfile.txt’); myfile.read() 

 

 

12. To open a file for reading any of the following statement can be used :

a) f = open(“demofile.txt”)

b) f = open(“demofile.txt”, “rt”)

c) f = open(“demofile.txt”, “r”)

d) All of the above 

d) All of the above 

 

13. Suppose content of ‘Myfile.txt’ is: Twinkle twinkle little star How I wonder what you are Up above the world so high Like a diamond in the sky What will be the output of the following code?

myfile = open(“Myfile.txt”)

data = myfile.readlines()

print(len(data))

myfile.close()

a) 3

b) 4 

c) 5

d) 6

b) 4

 

 

14. Suppose content of ‘Myfile.txt’ is Culture is the widening of the mind and of the spirit. What will be the output of the following code?

myfile = open(“Myfile.txt”)

x = myfile.read()

y = x.count(‘the’)

print(y)

myfile.close()

a) 2

b) 3 

c) 4

d) 5

b) 3 

 

Don’t Miss Topics:

50+ Most Important Text File MCQ for Term 1 (Including CASE BASED MCQ)

60+ Most Important Binary File MCQ for Term 1 (Including CASE BASED MCQ)

40+ Most Important CSV File MCQ for Term 1(Including CASE BASED MCQ)

 

CASE STUDY QUESTIONS 

15. Atif has been asked by his senior to complete the following code. The code uses a text file namely message.txt.

def count_words(filename):

Bigwords=0

F=open(filename,‟r‟)

Data=F.___________ # line 1

words = _____________ # line 2

For w in words :

__________ # line 3

___________# line 4

return bigwords, len(words)

_______ # line 5

print(“total number of words : “,count)

print(“No. of big words :”,big)

 

15.1) Which function is used to read all the content of the file for line1 and store in Data variable in string format.

a) readline()

b) read(file)

c) read() 

d) readlines()

c) read() 

 

15.2)Which option is correct for completing line 2

a) Data.split() 

b) Data.Split()

c) f.split()

d) None of the above

a) Data.split() 

 

15.3) Which option is correct for completing line 3 and line 4 so that the count of words having length more than 8 characters.

a) If w>8 :

bigwords+=1

 

b) if len(w) >8:

bigwords+=1 

 

c) if len(w) >=8:

bigwords+=1

 

d) if len(w) >8:

bigwords+=1

b) if len(w) >8:

bigwords+=1 

 

 

15.4) Which option is correct for completing line 5 so that function count_words() is called.

a) big ,count= count_words(filename)

b) big ,count= count_words(“message.txt”) 

c) big _count=count_words(“message.txt”)

d) big=count_words(“message.txt”) count=count_words(“message.txt”)

b) big ,count= count_words(“message.txt”)

 

 

16. Teacher has developed a program to count how many lines start with “T” in a text file “story.txt”. To choose correct option for executing program successfully.

def countlines():

file= _______ (“story.txt”) #line1

data = __________ #line2

__________# line 3

for I in data :

if _______ : # line4

c+=1

print(“Number of lines start with T “, _____) #line5

 

16.1) Which function to be used to complete line 1

a) Read()

b) open() 

c) read()

d)Open()

b) open() 

 

 

16.2) Which function is used to read the content from the file

a) file.read()

b) readline()

c) file.readlines() 

d) file.readline()

c) file.readlines()

 

 

16.3) Line3- initialize the variable for count

a) c=0 

b) c=c+1

c) both

d) none

a) c=0 

 

 

16.4) choose correct condition for line3

a) i==’T’

b) i[0]==’T’ 

c) “i”==T

d) i[0]==T

b) i[0]==’T’

 

 

16.5) complete the line4

a) c 

b) count

c) I

d) data

a) c 

 

 

17. Anisha wants to develop a program to count the number of “Me” or “My” words present in a text file “STORY.TXT” .But she is not sure about the code . Help her to complete it.

def displayMeMY(): n=0 f=open(“story.txt” , „____‟) #line 1

N = __read() #line2

M = N.________ #line 3

for x in M :

if ____________________: #line 4

n=n+1 f.________ #line 5 Closing the file.

print(“Count of Me /My words : “, ____) #line 6

 

17.1) Which access mode to be used in line 1 to open the file .

a) w

b) r 

c) rb

d) a

b) r 

 

 

17.2) Fill the line 2 by suitable option

a) F.

b) f. 

c) n.

d) N.

b) f. 

 

 

17.3) Which method to be used to complete the line 3

a) readline()

b) split() 

c) write()

d) writelines()

b) split() 

 

 

17.4) select the correct option to complete the line 4

a) x==me or x== my

b) x==”me” or “my”

c) x==”Me” or x==”My” 

d) x==[“Me”,”My”]

c) x==”Me” or x==”My”

 

 

17.5) Which function is used to complete line 5.

a) Exit()

b) close() 

c) end()

d) Close()

b) close()

 

 

18. Rahul is trying to perform write the data to a text file. He is not sure of the commands to be used. Hence partially he could write the program . Help him to complete the program.

file1 = open(“myfile.txt”, ____)#line1

L = [“This is Delhi \n”, “This is Paris \n”, “This is London”]

file1._________(L) #line2

file1.close()

file1 = open(“myfile.txt”, _____)#line3 opening file to add new data to existing file .

# writing newline character

file1.write(“\n”)

file1._________(“Today”)#line4

 

18.1) Which access mode to be used in line 1 to open the file for writing on to file.

a) w 

b) w+

c) wr+

d) a

a) w 

 

18.2) Which function to be used in line 2 for writing a list to file.

a) write()

b) writelines() 

c) writerow()

d) writeline()

b) writelines() 

 

 

18.3) Which access mode to be used in line3 to open the file for writing new content on existing file without any data loss.

a) w

b) w+

c) wr+

d) a 

d) a 

 

 

18.4) Which function to be used in line4 for writing a list to file.

a) write() 

b) writelines()

c) writerow()

d) writeline()

a) write() 

 

 

19) Smitha wants to copy text file “Source.txt” onto “target.txt” barring the lines starting with a “@” sign. Help her in completing the code. def filter(oldfile, newfile):

fin=open(oldfile,___)#line1

fout=open(newfile,_____)#line2

while True :

text=__________#line3

if len(text)==0:

break

if text[0]==”@” :

continue

_____.write(text)#line4

fin._____ #line5

fout._____#line6

filter(“source.txt”,”target.txt”)

 

19.1) In which access mode oldfile to be opened. Complete the line 1 by selecting appropriate option.

a) w

b) r 

c) rb

d) a

b) r 

 

 

19.2) In which access mode newfile to be opened. Complete the line 2 by selecting appropriate option.

a) w 

b) r

c) rb

d) a

a) w 

 

 

19.3) Which of the following function can be used to complete line3 for reading line by line.

a) fin.readline() 

b) fin.readlines()

c)fin.read()

d) fout.readline()

a) fin.readline() 

 

 

19.4) Identify the object that can be used in line4

a) fin

b) fout 

c) Fin

d) Fout

b) fout 

 

 

19.5) Identify the function that can be used in line5 and 6 to close the files

a) closed()

b) close() 

c) end()

d)eol()

b) close() 

 

 

20. Write the output of the following code

f = open(“data.txt”, “r”)

d =read()

d1 = read(5)

print(d)

print(d1)

#data file contains the following data

Welcome to python program

a) Welcome to python program

b) Welco

c) error 

d) None

c) error 

 

 

21. Your teacher has given you a method/function FilterWords() in python which reads data from a text file NewsLetter.TXT. Your teachers intentionally kept few blanks in between the code and asked you to fill the blanks so that the code will run to find desired result. Do the needful with the following python code.

def FilterWords():

c=0

file=open(‘NewsLetter.TXT’, ‘_____’) #Statement-1

data = file._____ #Statement-2

line = _____ #Statement-3

linecount= _____: #Statement-4

print(“No of lines”,linecount)

_________ #Statement-5

FilterWords()

 

21.1) Fill in the statement 1 with appropriate access mode

a) rb

b) r 

c) w

d)a

b) r 

 

 

21.2) Fill the statement 2 with appropriate function to read 5 characters

a) read()

b) read(5) 

c) readline(5)

d) get(5)

b) read(5) 

 

 

21.3) Fill the statement 3 to read the remaining content of the file in list form.

a) file.read()

b) file.readlines() 

c) file.readline()

d) readlines()

b) file.readlines() 

 

 

21.4) Fill the statement 4 to count the no. of lines in the file.

a) len()

b) len(line) 

c) Len(line)

d) len.line

b) len(line) 

 

 

21.5) Fill in the blank in Statement-5 to close the file.

a) file.close() 

b) File.Close()

c) Close()

d) end()

a) file.close() 

 

 

22. Renu wants to write multiple lines i.e 5 lines of text content into a text file mylife.txt.But she is not able to do it. Help her to complete the program.

F =open( ___________) #line 1

________________ #line 2

Line = ____________________#line 3

_____________ #line 4

_____________ #line 5

f.close()

 

22.1) Fill line 1 with correct statement.

a) (“mylife.txt”)

b) (“mylife.txt”,”w”) 

c) (“mylife.txt”, “ wb”)

d) (mylife.txt)

b) (“mylife.txt”,”w”) 

 

 

22.2) Choose the correct option for looping to fill line2

a) For I in range(5):

b) for I in range(1,5):

c) for I in range(5): 

d) for I in range(5)

c) for I in range(5): 

 

 

22.3) Fill line 3 with correct option to take the input from console IO.

a) Input(“enter a line”)

b) input(“enter a line”) 

c) str(“Enter a line”)

d) int(input((“enter a line”) )

b) input(“enter a line”) 

 

 

22.4) Fill line 4 with correct option to copy the line to file.

a) write(Line)

b) writeline(Line)

c) F.write(“Line”)

d) F.write(Line) 

d) F.write(Line) 

 

 

22.5) Fill line 5 with correct option to add new line character after every line.

a) write(“ ”)

b) writeline(“ ”)

c) F.write(“\n”) 

d) write(“\n”)

c) F.write(“\n”) 

 

 

23. Sonu wants to create one single program to read and write data using a single file object.But she got confused and could not complete the program. Help her to complete it .

fileobject=open(“report.txt”, “____”) # line 1

print (“WRITING DATA IN THE FILE”)

print() # to display a blank line

while True:

line= input(“Enter a sentence “)

fileobject.___________ #line2

fileobject.write(‘\n’)

choice=input(“Do you wish to enter more data? (y/n): “)

if choice in (‘n’,’N’):

break

print(“The byte position of file object is “,fileobject.tell())

fileobject._________ # line3

print()

print(“READING DATA FROM THE FILE”)

str=_______________ #line4

print(str)

___________ #line 5

 

23.1) Fill the line1 with appropriate access mode to perform both write and read operations.

a) r+

b) w

c) w+ 

d) a

c) w+ 

 

 

23.2) Fill Line 2 to perform write operation on to the file .

a) Writeline(line)

b) write(line) 

c) writelines(line)

d) writerow(line)

b) write(line) 

 

 

23.3) Fill line 3 with correct function that places file object at beginning of file

a) tell()

b) seek(0) 

c) Seek(0)

d) seek()

b) seek(0) 

 

 

23.4) Fill line 4 to read to all the content of the file.

a) fileobject.read() 

b) read()

c) fileobject.readline()

d) readrow()

a) fileobject.read() 

 

 

23.5) Fill line 5 to close the file.

a) fileobject.close() 

b) Fileobject.Close()

c) Close()

d) end()

a) fileobject.close() 

 

24. Mr.Ravi, has written code which reads each character of a text file story.txt,and also counts and displays the occurance of alphabets of A (include small cases „a‟ too).But while doing so ,he is unable to complete the code ,help him to complete the code.

Ex : If the file content is as follows:

Updated information

As simplified by official websites.

The function EUcount() will display the output as :

A or a :4

M or m : 2

Code :

def EUcount():

________________ #1 statement to open the file

A=0

_________________ #2 statement to read the data from the file

for x in r:

_________________# 3 statement

A=A+1

42

f.close()

print(“A or a:”,A)

 

24.1) Which of the following commands is used to open the file “Story.txt”? (marked as #1 in the Python code)

a) f= open(“story.txt”,’w’)

b) f= open (“story.txt “,’r’) 

c) f= open (“story.txt “,’r+’)

d) f= open (“story.txt “,’rb’)

b) f= open (“story.txt “,’r’) 

 

 

24.2)Which of the following commands is used to read the data from the file, Story.txt? (marked as #2 in the Python code)

a) f.read( 20)

b) f.read() 

c) f.write(L,F)

d) f=pickle.dump(L)

b) f.read() 

 

 

24.3)Which of the following commands is used to compare alphabets of A (include small cases “a” too).

a) if x==‟A‟ or x==”a”: 

b) if x==‟A‟ or ”a”:

c) if x=‟A‟ or x=”a”:

d) if x in ‟A‟ or ”a”:

a) if x==‟A‟ or x==”a”: 

 

MCQ Seek and tell methods, manipulation of data in a text file

25. Which function is used to change the position of the File Handle to a given specific position.

a) seek() 

b) read()

c) tail()

d) write()

a) seek() 

 

 

26. Seek() function with negative offset only works when file is opened in ——– mode.

a) read mode

b) write mode

c) binary mode 

d) All of these

c) binary mode

 

 

27. What is the use of seek() method in files?

a) sets the file‟s current position at the offset

b) sets the file‟s previous position at the offset

c) sets the file‟s current position within the file

d) none of the mentioned

a) sets the file‟s current position at the offset

 

 

28. How do you get the current position within the file?

a) fp.seek()

b) fp.tell() 

c) fp.loc

d) fp.pos

b) fp.tell() 

 

 

29. How do you change the file position to an offset value from the start?

a) fp.seek(offset, 0) 

b) fp.seek(offset, 1)

c) fp.seek(offset, 2)

d) none of the mentioned

a) fp.seek(offset, 0) 

 

 

30. What happens if no arguments are passed to the seek function?

a) file position is set to the start of file

b) file position is set to the end of file

c) file position remains unchanged

d) error 

d) error 

 

 

31. If we open the file in read mode then the initial value of tell() is

a) -1 

b) 0 

c) 1

d) End of the file

a) -1 

 

 

32. How many arguments passed to the tell()?

a) 0 

b) 1

c) 2

d) variable number of arguments

a) 0 

 

33. Which is the correct statement with respect of seek() function?

a) 0: sets the reference point at the beginning of the file

b) 1: sets the reference point at the current file position

c) 2: sets the reference point at the end of the file

d) All of these 

d) All of these  

 

34. What is the use of seek() method in files()?

a) sets the file’s current position at the offset 

b) sets the file’s previous position at the offset

c) sets the file’s current position within the file

d) none of the mentioned

a) sets the file’s current position at the offset 

 

 

35. Which of the following statements correctly explain the function of tell() method?

a) tells the current position within the file. 

b) indicates that the next read or write will occur at that many bytes from the beginning of the file.

c) move the current file position to a different location.

d) it changes the file position only if allowed to do so else returns an error.

a) tells the current position within the file. 

b) indicates that the next read or write will occur at that many bytes from the beginning of the file.

 

 

36. Which of the following statements correctly explain the function of seek() method?

a) tell the current position within the file.

b) indicate that the next read or write occurs from that position in a file.

c) determine if you can move the file position or not.

d) move the current file position to a different location at a defined offset. 

d) move the current file position to a different location at a defined offset.

 

37. Which of the following command is used to open a file “c:\temp.txt” in read-mode only?

a) infile = open(“c:\temp.txt”, “r”)

b) infile = open(“c:\\temp.txt”, “r”) 

c) infile = open(file = “c:\temp.txt”, “r+”)

d) infile = open(file = “c:\\temp.txt”, “r+”)

b) infile = open(“c:\\temp.txt”, “r”)

 

 

38. To read the next line of the file from a file object fobj, we use:

a) fobj.read(2)

b) fobj.read()

c) fobj.readline() 

d) fobj.readlines()

c) fobj.readline()

 

 

39. What happens if no arguments are passed to the seek function?

a) file position is set to the start of file

b) file position is set to the end of file

c) file position remains unchanged

d) error 

d) error

 

 

40. What does the <readlines()> method returns?

a) str

b) a list of lines 

c) list of single characters

d) list of integers

b) a list of lines 

 

 

41. Which of the following command is used to open a file “c:\temp.txt” in append-mode?

a) outfile = open(“c:/temp.txt”, “a”) 

b) outfile = open(“c:\\temp.txt”, “rw”)

c) outfile = open(“c:\temp.txt”, “w+”)

d) outfile = open(“c:\\temp.txt”, “r+”)

a) outfile = open(“c:/temp.txt”, “a”) 

 

 

42. Which of the following commands can be used to read “n” number of characters from a file using the file object <file>?

a) file.read(n) 

b) n = file.read()

c) file.readline(n)

d) file.readlines()

a) file.read(n) 

 

 

43. Which of the following functions can be used to check if a file “logo” exists?

a) os.path.isFile(logo)

b) os.path.exists(logo)

c) os.path.isfile(logo) 

d) os.isFile(logo)

c) os.path.isfile(logo) 

 

 

 

Don’t Miss Topics:

50+ Most Important Text File MCQ for Term 1 (Including CASE BASED MCQ)

60+ Most Important Binary File MCQ for Term 1 (Including CASE BASED MCQ)

40+ Most Important CSV File MCQ for Term 1(Including CASE BASED MCQ)

By cbsepython

A complete solution for the students of class 9 to 12 having subject Information Technology (402), Computer Science (083). Explore our website for all the useful content as Topic wise notes, Solved QNA, MCQs, Projects and Quiz related to the latest syllabus.

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved