Must Prepare Python File Handling MCQ Class 12 For Term 1
Must Prepare Python File Handling MCQ Class 12 For Term 1
CBSE
Learn CBSE PYTHON
Menu
CBSE > Class 12 > Must Prepare Python File Handling MCQ Class 12 for Term 1
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.
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
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
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Couldn’t put Humpty together again
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?
Show Answer
11. Which of the following options can be used to read the whole content of a text le My le.txt?
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”)
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
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)
15. Atif has been asked by his senior to complete the following code. The code uses a text le namely message.txt.
Bigwords=0
F=open( lename,‟r‟)
Data=F.___________ # line 1
For w in words :
__________ # line 3
___________# line 4
_______ # line 5
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
a) Data.split()
b) Data.Split()
c) f.split()
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.
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():
__________# line 3
for I in data :
if _______ : # line4
c+=1
a) Read()
b) open()
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) read()
d)Open()
Show Answer
a) le.read()
b) readline()
c) le.readlines()
d) le.readline()
Show Answer
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
a) i==’T’
b) i[0]==’T’
c) “i”==T
d) i[0]==T
Show Answer
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.
N = __read() #line2
M = N.________ #line 3
for x in M :
if ____________________: #line 4
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
a) readline()
b) split()
c) write()
d) writelines()
Show Answer
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
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._________(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 .
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
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
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
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)
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
print(“No of lines”,linecount)
_________ #Statement-5
FilterWords()
a) rb
b) r
c) w
d)a
Show Answer
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
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.
________________ #line 2
Line = ____________________#line 3
_____________ #line 4
_____________ #line 5
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
f.close()
a) (“mylife.txt”)
b) (“mylife.txt”,”w”)
c) (“mylife.txt”, “ wb”)
d) (mylife.txt)
Show Answer
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 .
while True:
leobject.___________ #line2
leobject.write(‘\n’)
if choice in (‘n’,’N’):
break
leobject._________ # line3
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
print()
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
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
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
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.
Updated information
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
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’)
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
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
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)
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?
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
a) 0
b) 1
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) 2
Show Answer
d) All of these
Show Answer
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?
b) indicates that the next read or write will occur at that many bytes from the beginning of the le.
Show Answer
36. Which of the following statements correctly explain the function of seek() method?
b) indicate that the next read or write occurs from that position in a le.
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”)
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
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
c) le position remains unchanged
d) error
Show Answer
a) str
b) a list of lines
d) list of integers
Show Answer
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
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)
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 Operators
Python List
Python Tuple
Python Dictionary
Python Function
Python Datetime
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Python Tkinter
Latest Topics
Create PDF in your applications with the Pdfcrowd HTML to PDF API PDFCROWD
Term 1 Sample Paper Informatics Practices Class 12
Search …
Search
Search …
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