Please refer to Computer Science For File Handling in Python Class 12 Computer Science Exam Questions provided below. These questions and answers for Class 12 Computer Science have been designed based on the past trend of questions and important topics in your class 12 Computer Science books. You should go through all Class 12 Computer Science Important Questions provided by our teachers which will help you to get more marks in upcoming exams.
Class 12 Computer Science Exam Questions File Handling in Python
Very Short Answer Type Questions :
Question. One row of CSV file can be considered as _______ in terms of database
Answer: Record
Question. The writerow() function is a part of _________ module.
Answer: CSV
Question. A _____ function allows to write a single record into each row in CSV file.
Answer: writerow()
Question. Which of the following is not a function of csv module? 1. readline() 2. writerow() 3. reader() 4. writer()
Answer: readline()
Question. CSV module allows to write multiple rows using ____________ function.
Answer: writerrows()
Question. Write the full form of CSV
Answer: Comma Separated value
Question. Write the functions required to handle CSV files.
Answer: To handle CSV files following function required:
1. Open()
2. reader()
3. writer()
4. writerow()
5. close()
Question. The default line terminator is \n in csv file.
Answer: True
Question. A _________ is a file format which stores records separated by comma.
Answer: CSV
Question. Write to ways to import a csv module.
Answer: 1. import csv 2. from csv import *
Short Answer Type Questions :
Question. Write a python program to copy file1.txt into file2.txt
Answer:
fin=open(“file1.txt”)
fout=open(“file2.txt”,”w”)
data=fin.read()
fout.write(data)
fin.close()
fout.close()
Question. Which of the following function returns a list datatype
A) d=f.read()
B) d=f.read(10)
C) d=f.readline()
D) d=f.readlines()
Answer: f.readlines()
Question. Write a single loop to display all the contens of a text file file1.txt after removing leading and trailing WHITESPACES
Answer:
for line in open(“file1.txt”):
print(line.strip())
Question. Observe the following code and answer the follow
f1=open(“mydata”,”a”)
______#blank1
f1.close()
(i)what type of file is mydata
(ii) Fill in the blank1 with statement to write “abc” in the file “mydata”
Answer:
i) Text file
ii) f1.write(“abc”)
Question. Write a method in python to write multiple lines of text contents into a text file myfile.txt
Answer:
def write1():
f = open(“myfile.txt”,”w”)
while True:
line = input(“Enter line”)
f.write(line)
choice = input(“Are there more lines”)
if choice == “N”:
break
f.close()
Question. What would be the data types of variables data in following statements?
i) Data=f.read( )
ii) Data=f.read(10)
iii) Data=f.readline()
iv) Data=f.readlines()
Answer: a) string b)string c)string d)list
Question. A given text file data.txt contains :
Line1\n
\n
line3
Line 4
\n
line6
What would be the output of following code?
Answer:
Line1
Line3
Line 6
Line 4
Question. In which of the following file modes the existing data of the file will not be lost?
i) rb
ii) w
iii) a+b
iv) wb+
v) r+
vi) ab
vii) w+b
viii)wb
ix) w+
Answer: ab and a+b mode
Question. A text file “Quotes.Txt” has the following data written in it: Living a life you can be proud of Doing your best Spending your time with people and activities that are important to you Standing up for things that are right even when it’s hard
Becoming the best version of you Write a user defined function to display the total number of words present in the file.
Answer:
def countwords():
S=open(“Mydata”, “r”)
f=S.read()
z=f.split()
count=0 for
I in z:
count=count+1
print(“Total number of words”,count)