0% found this document useful (0 votes)
15 views6 pages

File Handling Revision

Uploaded by

Ranjana Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

File Handling Revision

Uploaded by

Ranjana Mishra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

NATIONAL PUBLIC SCHOOL WHITEFIELD

CLASS-12
TOPIC: FILE HANDLING & STACKS

1. Which of the following options is correct?


(a) Opening file in r+ mode means opening a file for reading only.
(b) When file is opened in r+ mode, the file pointer is placed at the beginning of the file.
(c) r+ is the default mode.
(d) all of the above
2. Which of the following options can be used to read the two lines of a text file in the form of
string from Myfile.txt. Assuming the file has two lines only.
(a) myfile = open(‘Myfile.txt’); myfile.read()
(b) myfile = open(‘Myfile.txt’,’r’); myfile.read(2)
(c) myfile = open(‘Myfile.txt’); myfile.readline(2)
(d) myfile = open(‘Myfile.txt’); myfile.readlines()
3. _______________ function closes the file and frees the memory space acquired by that file.
7. Which option is correct when file is opened in ‘a’ mode?
(a) Opens a file for appending.
(b) The file pointer is at the end of the file if the file exists.
(c) If the file does not exist, it creates a new file for writing.
(d) all of the above
4. Suppose content of ‘abc.txt’ is:
India is Great
I love my India
What will be the output of the following code?
f=open(‘abc.txt’,’r’)
data=f.readline(5)
print(data)
f.close()
5. Aman wants to read pickled file ELECTION.DAT and displays every such Name whose
Count is less than 10 but facing some problems to give the condition.
The File contains the following fields :
Name,Count
import pickle
def cLowCount():
file=open(‘ELECTION.DAT’,’rb’)
ERec=pickle.load(file) #To read the object from file
for E in ERec:
if __________: # statement 1
print (E[0] )
file.close()
6. Suppose content of ‘Myfile.txt’ is
You should always
close your files, in some
cases, due to buffering, changes
made to a file may not
show until you close the file
What will be the output of the following code?
myfile = open(“Myfile.txt”)
record = myfile.read().split(‘,’)
print(len(record))
myfile.close()

7. Divya has written a function countmy( )in Python to read the text file “DATA.TXT” and
count the number oftimes “my” occurs in the file. For example if the file “DATA.TXT”
contains: “This is my website. I have displayed my preferences in the CHOICE section.”
The countmy( ) function should display the output as “my occurs 2 times”
def countmy():
f=open(“DATA.TXT”,”r”)
k=f.read()
p=k.split()
_____________ #statement 1
print(“my occurs”,c,”times”)
f.close()
Select the appropriate statement to fill statement 1:
(a) c=k.count(“my”) (b) c=p.count[“my”] (c) p=k.count(“my”) (d) c=p.count(“my”)
8. A folder Test is created in root directory C:\. Two subfolders test1 ,test2 and one “x.txt”
is created in Test Folder. “y.txt” and “Try.py” are created in folder Test1 and “z.txt”
is created in Test2. Choose the correct option to write single line statement in “Try.py” to

open the “x.txt” file which is stored in folder Test using relative path.
(a) f=open(“x.txt”) (b) f=open(“.\\x.txt”)
(c) f=open(“test\\x.txt”) (d) f=open(“..\\x.txt”)
9. Write a function in Python PUSH(Arr), where Arr is a list of numbers. From this list push
all numbers which are prime into a stack implemented by using a list. Display the stack if
it has at least one element, otherwise display appropriate error message.
10. Arun, during Practical Examination of Computer Science, has been assigned an
incomplete search() function to search a record which is passed as parameter in a pickled
file student.dat. The File student.dat is created by his Teacher and the following
information is known about the file.
(a) File contains details of students in [roll_no,name,marks] format.
(b) File contains details of 10 students (i.e. from roll_no 1 to 10) and separate list of
each student is written in the binary file using dump().
Arun has been assigned the task of completing the code and print details of roll number1.
def search(r):
f = open(“student.dat”,____) #Statement-1
____: #Statement-2
while True:
rec = pickle.____ #Statement-3
if(____): #Statement-4
________ #Statement-5
except:
pass
____ #Statement-6
i. In which mode Arun should open the file I n Statement-1?
(a) r (b) r+ (c) rb (d) wb
ii. Identify the suitable code to be used at blank space in line marked as Statement2.
(a) Try (b) while True (c) try (d) pass

iii. Identify the function (with argument), to be used at blank space in line marked as
Statement-3.
(a) load() (b) f.load() (c) load(f) (d) f(load)
iv. What will be the suitable code for blank space in line marked as Statement-4.
(a) rec(0)==r (b) rec[1]==’r’ (c) rec[1]==r (d) rec[0]==r
v. What will be the suitable code for blank space in line marked as Statement-5 to show
record?
(a) show(rec) (b) print(rec) (c) print.rec() (d) rec(print)
vi. Which statement Arun should use at blank space in line marked as Statement5 to
close the file?
(a) file.close() (b) close(file) (c) f.close() (d) close()
11.
12.
13.

You might also like