Class-12-File Handling
Class-12-File Handling
Class-12-File Handling
split ()
Data file handling 1 mark questions computer science for i in L:
class 12 if len (i)%3!=0:
[1] Consider the following directory structure. print (i, end= " ")
computer science class 12 previous year questions file a) Baa baa you any
handling 1 mark questions b) black have wool?
Suppose the present working directory is MyCompany. c) black sheep, have wool?
What will be the relative path of the file Transactions.Dat? d) Error
a) MyCompany/Transactions.Dat
b) MyCompany/Accounts/Transactions.Dat [6] Suppose the content of a text file “Rhymes.txt” is as
c) Accounts/Transactions.Dat follows:
d) ../Transactions.Dat Jack & Jill
went up the hill
[2] Suppose the content of “Rhymes.txt” is: What will be the output of the following Python code ?
Hickory Dickory Dock F = open ("Rhymes.txt")
The mouse went up the clock L = F.readlines ()
F = open ("Rhymes. txt") for i in L:
L = F. readlines () S=i.split()
X = ["the", "ock"] print (len (S) ,end="#")
for i in L: a) 2#4#
for W in i.split () :
if W in X: b) 3#4#
print (W, end = "*")
a) the* c) 2#
b) Dock*The*the*clock*
c) Dock*the*clock* d) 7#
d) Error
[7] Which of the following function is used with the csv
[3] Suppose the content of “Rhymes.txt” is: module in Python to read of the contents a csv file into an
Good Morning Madam object?
What will be the output of the following Python code? a) readrow()
b) readrows()
F = open ("Rhymes.txt") c) reader()
L = F.read ().split () d) load()
for W in L: Ans.: c) reader()
if W.lower () == W[::-1].lower ():
print (W) [8] Which of the following Python modules is imported to
a) Good store and retrieve objects using the process of
b) Morning serialization and deserialization?
c) Madam a) csv
d) Error b) binary
c) math
[4] Suppose the content of “Rhymes.txt” is: d) pickle
One, two, three, four, five
Once. I caught a fish alive. [9] Suppose the content of a text file Notes.txt is:
What will be the output of the following Python code? The way to get started is to quit talking and begin doing
F = open ("Rhymes.txt") What will be the output of the following Python code?
S = F.read () F = open ("Rhymes.txt")
print (S.count('e',20)) F.seek (29)
a) 20 S= F.read ()
b) 1 print (S)
c) 3 a) The way to get started is to
d) 6 b) quit talking and begin doing
[5] Suppose the content of “Rhymes.txt” is: c) The way to get started is to quit talking and begin doing
Baa baa black sheep d) gniod nigeb dna gniklat tiuq ot si detrats teg ot yaw ehT
have you any wool? [10] Which of the following is the default character for the
What will be the output of the following Python code? newline parameter for a csv file object opened in write
F = open ("Rhymes.txt") mode in Python IDLE ?
S = F.read () a) \n
b) \t
c) , a) F = open(“Notes.txt”,”w”)
d) ;
b) F = open(“Notes.txt”,”a”)
[11] If the following statement is used to read the
contents of a textfile object F: X-F.readlines( ) c) F = open(“Notes.txt”,”A”)
Incomplete Code v. Identify the suitable code for blank space in the line
import _____ #Statement-1 marked as Statement-5.
fh = open(_____, _____, newline='') #Statement-2
stuwriter = csv._____ #Statement-3 a) data
data = [ ] b) record
header = ['ROLL_NO', 'NAME', 'CLASS', 'SECTION'] c) rec
data.append(header) d) insert
for i in range(5): Ans.: c) rec
roll_no = int(input("Enter Roll Number : "))
name = input("Enter Name : ")
vi. Choose the function name that should be used in the Ans.: b) for R in ER:
blank space of line marked as Statement-6 to create the
desired CSV File? v) Identify the suitable code for blank space in Statement-
a) dump() 5 to match every row’s 3rd property with “ACCOUNTS”.
b) load()
c) writerows() a) ER[3]
d) writerow() b) ER[2]
Ans.: d) writerow() c) R[2]
d) R[3]
[7] Nisha, an intern in ABC Pvt. Ltd., is developing a
project using the csv module in Python. She has partially Ans.: c) R[2]
developed the code as follows leaving out statements vi) Identify the suitable code for blank space in Statement-
about which she is not very confident. The code also 6 to display every Employee’s Name and corresponding
contains errors in certain statements. Help her in Department?
completing the code to read the desired CSV File named a) ER[1], R[2]
“Employee.csv”. b) R[1], ER[2]
c) R[1], R[2]
#CSV File Content d) ER[1], ER[2]
ENO, NAME, DEPARTMENT Ans.: c) R[1], R[2]
E1,ROSHAN SHARMA , ACCOUNTS
E2,AASMA KHALID, PRODUCTION
E3,AMRIK GILL,MARKETING
E4,SARAH WILLIAMS, HUMAN RESOURCE
#incomplete Code with Errors
import CSV #Statement-1
with open (______,_______ newline='') as. File:
#Statement-2
ER= csv. ________ #Statement-3
for R in range (ER) #Statement-4
if __________=="ACCOUNTs" #Statement-5
print(________) #Statement-6
i) Nisha gets an Error for the module name used in
Statement-1. What should she write in place of CSV to
import the correct module ?
a) file
b) csv
c) Csv
d) pickle
Ans.: b) csv
ii) Identify the missing code for blank spaces in the line
marked as Statement-2 to open the mentioned file.
a) “Employee.csv”, “r”
b) “Employee.csv”, “w”
c) “Employee.csv”, “rb”
d) “Employee.csv”, “w”
Ans.: a) “Employee.csv”,”r”
iii) Choose the function name (with parameter) that
should be used in the line marked as Statement-3.
a) reader (File)
b) readrows (File)
c) writer (File)
d) writerows (File)
Ans.: a) reader(File)
iv) Nisha gets an Error in Statement-4. What should she
write to correct the statement?
a) while R in range (ER) :
b) for R in ER:
c) for R = ER:
d) while R = ER: