0% found this document useful (0 votes)
1K views

Must Prepare Python File Handling MCQ Class 12 For Term 1

Multiple Choice Questions for practice. Important mcqs forr class XII Computer Science TERM_1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

Must Prepare Python File Handling MCQ Class 12 For Term 1

Multiple Choice Questions for practice. Important mcqs forr class XII Computer Science TERM_1
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Article: 80+ Important MCQ on  

CBSE
Learn CBSE PYTHON

 Menu

CBSE > Class 12 > Must Prepare Python File Handling MCQ Class 12 for Term 1

Must Prepare Python File Handling MCQ Class 12 for Term 1


 November 28, 2021  Jitendra Singh  Class 12, Python for CBSE

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 le as a list item.

a) read()

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
b) readline()

c) readlines() 

d) readone()

 Show Answer

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

a) write()

b) writelines()

c) writerow()

d) append()

 Show Answer

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

a) read() and readline() 

b) readlines() and readline()

c) read() and readlines()

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
d) None of the above

 Show Answer

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

a) read()

b) readline() 

c) readlines()

d) readone()

 Show Answer

5. To read twelve characters from a le object n we use ________

a) n.read(12) 

b) n.read()

c) n.readline()

d) read(12)

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 

6. Python automatically ushes the le buffers before closing a le with close() function.

a) True 

b) False

 Show Answer

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

a) close()

b) ush() 

c) clear()

d) open()

 Show Answer

8. Suppose content of ‘My le.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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Couldn’t put Humpty together again

What will be the output of the following code?

my le = open(“My le.txt”)

record = my le.read().split()

print(len(record))

my le.close()

a) 24

b) 25

c) 26 

d) 27

 Show Answer

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

a) “w+” 

b) “a+”

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) “r+”

d) “wb”

 Show Answer

10. Which of the following options can be used to read the rst line of a text le My le.txt?

a) my le = open(‘My le.txt’); my le.read()

b) my le = open(‘My le.txt’,’r’); my le.read(n)

c) my le = open(‘My le.txt’); my le.readline() 

d) my le = open(‘My le.txt’); my le.readlines()

 Show Answer

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

a) my le = open(‘My le.txt’); my le.read() 

b) my le = open(‘My le.txt’,’r’); my le.read(n)

c) my le = open(‘My le.txt’); my le.readline()

d) my le = open(‘My le.txt’); my le.readlines()

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 Show Answer

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

a) f = open(“demo le.txt”)

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

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

d) All of the above 

 Show Answer

13. Suppose content of ‘My le.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?

my le = open(“My le.txt”)

data = my le.readlines()

print(len(data))

my le.close()

a) 3

b) 4 

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) 5

d) 6

 Show Answer

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

my le = open(“My le.txt”)

x = my le.read()

y = x.count(‘the’)

print(y)

my le.close()

a) 2

b) 3 

c) 4

d) 5

 Show Answer

Don’t Miss Topics:

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
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 le namely message.txt.

def count_words( lename):

Bigwords=0

F=open( lename,‟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)

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
print(“No. of big words :”,big)

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

a) readline()

b) read( le)

c) read() 

d) readlines()

 Show Answer

15.2)Which option is correct for completing line 2

a) Data.split() 

b) Data.Split()

c) f.split()

d) None of the above

 Show Answer

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.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
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

 Show Answer

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

a) big ,count= count_words( lename)

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

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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
d) big=count_words(“message.txt”) count=count_words(“message.txt”)

 Show Answer

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

def countlines():

le= _______ (“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() 

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) read()

d)Open()

 Show Answer

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

a) le.read()

b) readline()

c) le.readlines() 

d) le.readline()

 Show Answer

16.3) Line3- initialize the variable for count

a) c=0 

b) c=c+1

c) both

d) none

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 Show Answer

16.4) choose correct condition for line3

a) i==’T’

b) i[0]==’T’ 

c) “i”==T

d) i[0]==T

 Show Answer

16.5) complete the line4

a) c 

b) count

c) I

d) data

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
17. Anisha wants to develop a program to count the number of “Me” or “My” words present in a text le “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 le.

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

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

a) w

b) r 

c) rb

d) a

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
17.2) Fill the line 2 by suitable option

a) F.

b) f. 

c) n.

d) N.

 Show Answer

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

a) readline()

b) split() 

c) write()

d) writelines()

 Show Answer

17.4) select the correct option to complete the line 4

a) x==me or x== my

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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) x==”Me” or x==”My” 

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

 Show Answer

17.5) Which function is used to complete line 5.

a) Exit()

b) close() 

c) end()

d) Close()

 Show Answer

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

le1 = open(“my le.txt”, ____)#line1

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

le1._________(L) #line2

le1.close()

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
le1 = open(“my le.txt”, _____)#line3 opening le to add new data to existing le .

# writing newline character

le1.write(“\n”)

le1._________(“Today”)#line4

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

a) w 

b) w+

c) wr+

d) a

 Show Answer

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

a) write()

b) writelines() 

c) writerow()

d) writeline()

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 Show Answer

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

a) w

b) w+

c) wr+

d) a 

 Show Answer

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

a) write() 

b) writelines()

c) writerow()

d) writeline()

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
19) Smitha wants to copy text le “Source.txt” onto “target.txt” barring the lines starting with a “@” sign. Help her in completing
the code. def lter(old le, new le):

n=open(old le,___)#line1

fout=open(new le,_____)#line2

while True :

text=__________#line3

if len(text)==0:

break

if text[0]==”@” :

continue

_____.write(text)#line4

n._____ #line5

fout._____#line6

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

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

a) w

b) r 

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) rb

d) a

 Show Answer

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

a) w 

b) r

c) rb

d) a

 Show Answer

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

a) n.readline() 

b) n.readlines()

c) n.read()

d) fout.readline()

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 Show Answer

19.4) Identify the object that can be used in line4

a) n

b) fout 

c) Fin

d) Fout

 Show Answer

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

a) closed()

b) close() 

c) end()

d)eol()

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
20. Write the output of the following code

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

d =read()

d1 = read(5)

print(d)

print(d1)

#data le contains the following data

Welcome to python program

a) Welcome to python program

b) Welco

c) error 

d) None

 Show Answer

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

def FilterWords():

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c=0

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

data = le._____ #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

 Show Answer

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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
a) read()

b) read(5) 

c) readline(5)

d) get(5)

 Show Answer

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

a) le.read()

b) le.readlines() 

c) le.readline()

d) readlines()

 Show Answer

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

a) len()

b) len(line) 

c) Len(line)

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
d) len.line

 Show Answer

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

a) le.close() 

b) File.Close()

c) Close()

d) end()

 Show Answer

22. Renu wants to write multiple lines i.e 5 lines of text content into a text le 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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
f.close()

22.1) Fill line 1 with correct statement.

a) (“mylife.txt”)

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

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

d) (mylife.txt)

 Show Answer

22.2) Choose the correct option for looping to ll 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)

 Show Answer

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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
a) Input(“enter a line”)

b) input(“enter a line”) 

c) str(“Enter a line”)

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

 Show Answer

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

a) write(Line)

b) writeline(Line)

c) F.write(“Line”)

d) F.write(Line) 

 Show Answer

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

a) write(“ ”)

b) writeline(“ ”)

c) F.write(“\n”) 

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
d) write(“\n”)

 Show Answer

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

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

print (“WRITING DATA IN THE FILE”)

print() # to display a blank line

while True:

line= input(“Enter a sentence “)

leobject.___________ #line2

leobject.write(‘\n’)

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

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

break

print(“The byte position of le object is “, leobject.tell())

leobject._________ # line3

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
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

 Show Answer

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

a) Writeline(line)

b) write(line) 

c) writelines(line)

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
d) writerow(line)

 Show Answer

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

a) tell()

b) seek(0) 

c) Seek(0)

d) seek()

 Show Answer

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

a) leobject.read() 

b) read()

c) leobject.readline()

d) readrow()

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 

23.5) Fill line 5 to close the le.

a) leobject.close() 

b) Fileobject.Close()

c) Close()

d) end()

 Show Answer

24. Mr.Ravi, has written code which reads each character of a text le 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 le content is as follows:

Updated information

As simpli ed by o cial websites.

The function EUcount() will display the output as :

A or a :4

M or m : 2

Code :

def EUcount():

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
________________ #1 statement to open the le

A=0

_________________ #2 statement to read the data from the le

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 le “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’)

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
24.2)Which of the following commands is used to read the data from the le, 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)

 Show Answer

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”:

 Show Answer

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 speci c position.

a) seek() 

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
b) read()

c) tail()

d) write()

 Show Answer

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

a) read mode

b) write mode

c) binary mode 

d) All of these

 Show Answer

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

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

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

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

d) none of the mentioned

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 Show Answer

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

a) fp.seek()

b) fp.tell() 

c) fp.loc

d) fp.pos

 Show Answer

29. How do you change the le 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

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
30. What happens if no arguments are passed to the seek function?

a) le position is set to the start of le

b) le position is set to the end of le

c) le position remains unchanged

d) error 

 Show Answer

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

a) -1 

b) 0 

c) 1

d) End of the le

 Show Answer

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

a) 0 

b) 1

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) 2

d) variable number of arguments

 Show Answer

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

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

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

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

d) All of these 

 Show Answer

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

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

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

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

d) none of the mentioned

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 Show Answer

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

a) tells the current position within the le. 

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

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

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

 Show Answer

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

a) tell the current position within the le.

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

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

d) move the current le position to a different location at a de ned offset. 

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
37. Which of the following command is used to open a le “c:\temp.txt” in read-mode only?

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

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

c) in le = open( le = “c:\temp.txt”, “r+”)

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

 Show Answer

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

a) fobj.read(2)

b) fobj.read()

c) fobj.readline() 

d) fobj.readlines()

 Show Answer

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

a) le position is set to the start of le

b) le position is set to the end of le

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) le position remains unchanged

d) error 

 Show Answer

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

a) str

b) a list of lines 

c) list of single characters

d) list of integers

 Show Answer

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

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

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

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

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

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 Show Answer

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

a) le.read(n) 

b) n = le.read()

c) le.readline(n)

d) le.readlines()

 Show Answer

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

a) os.path.isFile(logo)

b) os.path.exists(logo)

c) os.path.is le(logo) 

d) os.isFile(logo)

 Show Answer

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
 

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)

Previous Class 10 IT 402 Sample Paper Term 1

Next 150+ MCQ Revision Tour of python class 12

Leave a Reply
Your email address will not be published. Required elds are marked *

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Comment

Name *

Email *

Website

Save my name, email, and website in this browser for the next time I comment.

Post Comment

Python Tutorial
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Python – Getting started

Python Variable

Python Keyword & Identifiers

Python Operators

Python Number Data Type

Python String Data Type

Python while loop

Python for loop

Python if….else statements

Python Jump Statement

Python List

Python Tuple

Python Dictionary

Python Function

Python File Handling

STANDARD PYTHON LIBRARIES

Python Datetime

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Python Tkinter

Latest Topics

80+ Important MCQ on Computer Network Class 12

50+ Important Data Structure MCQ for Class 12

50+ MCQ on Working with functions in python Class 12

150+ MCQ Revision Tour of python class 12

Must Prepare Python File Handling MCQ Class 12 for Term 1

Class 12 Computer Science Sample Paper 2021-22 Term 1

Computer Science Sample Paper Class 11 2021-22 Term 1

Computer Science Sample Paper Class 12 2021-22 Term 1

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Term 1 Sample Paper Informatics Practices Class 12

100+ Text File MCQ Question Bank For Class 12

Search … 

Search

Search … 

 

Copyright © All rights reserved.

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
University Hub by WEN Themes

Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD

You might also like