Font size
WorksheetsComputer science file handling Grade 12 cbse
Total questions: 80
Worksheet time: 38mins
What is the process of reading data from a text file called?
data extraction
text scanning
file reading
file parsing
How can you open a text file in read mode in Python?
open('file.txt', 'r')
open('file.txt', 'w')
open('file.txt', 'x')
open('file.txt', 'a')
What is the main advantage of using binary files over text files?
Efficiency in storage and processing speed
Higher level of security for sensitive data
Easier to edit and modify the content
Better compatibility with different operating systems
Explain the concept of newline in CSV files.
A newline in CSV files is represented by the character sequence '\v'.
A newline in CSV files is represented by the character sequence '\n'.
A newline in CSV files is represented by the character sequence '\r'.
A newline in CSV files is represented by the character sequence '\t'.
What is the purpose of the delimiter in CSV files?
To format the text in bold
To add color to the file
To separate individual fields or columns within each row
To encrypt the data
How can you write data to a text file in Python?
Skip closing the file after writing
Open the file in read mode: file = open('filename.txt', 'r')
Open the file in write mode: file = open('filename.txt', 'w') Write data to the file: file.write('Hello, World!') Close the file: file.close()
Use file.append() method to write data
In CSV files, what does each row represent?
Each row represents a single record or data entry.
Each row represents a header
Each row represents a paragraph
Each row represents a column
What are the different modes in which you can open a file in Python?
x
b
z
'r', 'w', 'a', 'r+', 'w+', 'a+'
How can you handle errors while working with files in Python?
Use if-else statements to handle errors when working with files.
Use try-except blocks in Python to handle errors when working with files.
Ignore errors and continue execution when working with files.
Manually terminate the program when errors occur while working with files.
What is the difference between reading and writing to a file?
Reading retrieves data, writing adds or updates data.
Reading deletes data, writing appends data.
Reading updates data, writing retrieves data.
Reading adds data, writing retrieves and updates data.
Explain the process of writing data to a binary file.
The process involves opening the binary file, converting data to binary format, writing the binary data, and then closing the file.
Skipping the step of closing the file after writing
Writing the data in reverse order
Converting data to text format before writing
What is the significance of the 'rb' mode when opening a binary file?
It indicates that the file is being opened in write mode for binary data
It signifies that the file is being opened in append mode for binary data
It specifies that the file is being opened in read mode for text data
It signifies that the file is being opened in read mode specifically for binary data.
How can you specify a custom delimiter while reading a CSV file in Python?
csv.reader(file, delim='custom_delimiter')
csv.reader(file, delimiter='custom_delimiter')
csv.reader(file, delimiter='custom_delimiter')
csv.reader(file, delimiter='custom_delim')
Discuss the importance of closing a file after performing operations on it.
Closing a file after performing operations on it helps release system resources and ensures data integrity and security.
Leaving a file open improves system performance
Not closing a file enhances file security
Closing a file can lead to data loss
What is the purpose of using 'rb' mode when opening a binary file in Python?
It indicates that the file is being opened in write mode for binary data
It signifies that the file is being opened in append mode for binary data
It specifies that the file is being opened in read mode for text data
It signifies that the file is being opened in read mode specifically for binary data.
What is the purpose of the 'a' mode when opening a file in Python?
It indicates that the file is being opened in append mode for text data
It signifies that the file is being opened in read mode for binary data
It specifies that the file is being opened in write mode for text data
It signifies that the file is being opened in read mode specifically for text data.
Explain the difference between 'r' and 'w' modes when opening a file in Python.
'r' mode opens the file in read-only mode, while 'w' mode opens the file in write-only mode.
'r' mode opens the file in write-only mode, while 'w' mode opens the file in read-only mode.
'r' mode opens the file in read and write mode, while 'w' mode opens the file in write-only mode.
'r' mode opens the file in read-only mode, while 'w' mode opens the file in read and write mode.
the module to be imported to work with binary files in python
(a)
Explain the difference between 'a' and 'w' modes when opening a file in Python.
'a' mode opens the file in append mode, while 'w' mode opens the file in write-only mode.
'a' mode opens the file in write-only mode, while 'w' mode opens the file in read-only mode.
'a' mode opens the file in append mode, while 'w' mode opens the file in write-only mode.
'a' mode opens the file in read and write mode, while 'w' mode opens the file in write-only mode.
What is the difference between appending and writing to a file?
Appending adds data at the end of the file, while writing overwrites existing data.
Appending deletes data, while writing updates data.
Appending updates data, while writing retrieves data.
Appending adds data, while writing retrieves and updates data.
What will be the output-
f=open('abc.txt','a')
text="xyz"
f.write(text)
f.close()
Writing into the file without erasing old content
Writing into the file by erasing old content
reading from the file without erasing old content
reading from the file by erasing old content
Mode in the following program to open file for writing in a mode where new data will be added at the end of old data
file =open("poem.txt","__")
r
w
a
r+
Choose the correct statement to close a file stream named myfile:
myfile.terminate()
myfile.end()
myfile.close()
myfile.remove()
Fill in the blank with appropriate statement.
(a)
Write the appropriate statement in the blank.
(a)
Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing.
file =open("Record.dat","wb")
file =open("Record.dat","rb")
file =open("Record.dat","ab")
file =open("Record.dat","rb+")
Which function read single line at a time?
read()
read(n)
readline()
readlines()
Which file is not binary file?
student.mp4
student.jpeg
student.csv
student.exe
Where files are store permanently?
RAM
Hard Disk
Cache memory
None of these
Which function is used to write multiple lines in text file?
write()
writelines()
lines()
print()
Which statement(s) is/are true to open a binary file "Record.dat" for reading and writing ?
file =open("Record.dat","wb")
file =open("Record.dat","rb")
file =open("Record.dat","ab")
file =open("Record.dat","rb+")
Q.2 Which of the following statements are not true regarding the opening modes of a file?
When you open a file for reading, if the file does not exist, an error occurs.
When you open a file for writing, if the file does not exist, an error occurs
When you open a file for writing, if the file does not exist, a new file is created.
When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
Q.3 Which module is used to work with binary files?
CSV module
pickle module
math module
binary module
Function used to write data in binary format
write()
output()
load()
dump()
(a)
see the program and fill in the blank with correct statement.
(a)
write statement to use the required module for working with comma separated values (csv) files.
(a)
Write the appropriate statement in the blank.
(a)
Information stored on a storage device with a specific name is called a _______________
tuple
array
dictionary
file
To read the next line of the file from a file object called F_H, we use
F_H.readlines()
F_H.read(all)
F_H.readline()
F_H.read()
Which of the following format of files can be created programmatically through python to store some data
Data File
Video File
Binary File
Text File
To read the remaining line of the file from a file object called F_H, we use
F_H.readlines()
F_H.read.remaining()
F_H.readline()
F_H.read(remaining)
The readlines() method returns_______
String
A list of characters
A list of lines
Index of lines in as a list
Which of the following code of line is with error?
Out = open("d:\\PyPro\\test.txt", "r")
Out = open(r"d:\PyPro\test.txt", "r")
Out = open(path="d:\\PyPro\\test.txt", "r")
Out = open("test.txt", "r")
Which of the statement is not true regarding the opening mode of a file?
When you open a file for reading, if the file does not exist, an error occurs.
When you open a file for writing, if the file does not exist, an error occurs.
When you open a file for reading in r+ mode, if the file does not exist, an error occurs.
When you open a file for reading in w+ mode, if the file does not exist, a new file is created.
What is the use of seek() method in files?
sets the file’s current position at the offset
sets the file’s previous position at the offset
sets the file’s current position within the file
none of the mentioned
EOL stands for
End Of Line
End Of List
End of Lines
End Of Location
In f=open(“data.txt”, “r”), r refers to __________.
File handle
File object
File Mode
Buffer
Q1. Given the following file opened as song_file:
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
What is the result of
print(song_file.read(4)) ?
I ca
I can
I c
I can't write one song that's not about you
Can't drink without thinkin' about
I c
you
Is it too late to tell you that
Everything means nothing if I can't have you?
Given the following file opened as song_file:
I can't write one song that's not about you
Can't drink without thinkin' about you
Is it too late to tell you that
Everything means nothing if I can't have you?
What is the result of
print(text_file.readlines()[1]) ?
I can't write one song that's not about you
Can't drink without thinkin' about you
I can't write one song that's not about you
Can't drink without thinkin' about
you
Is it too late to tell you that
Everything means nothing if I can't have you?
I
What is the best way to read an entire file into a list of strings?
.read()
.readline()
.readlines()
.read(all)
What is the best way to read an entire file into a single string?
.read()
.readline()
.readlines()
.read(all)
What is the best way to read the first line of a file?
.read()
.readline()
.readlines()
.read(all)
When reading text files, what are the hidden characters typically at the end of each line?
\n
»
#
@
If I want to add to the end of a text file, I should:
MyCipher = open("MyCipher.txt", "w")
MyCipher = open("MyCipher.txt", "a")
MyCipher = open("MyCipher.txt", "w+")
MyCipher = open("MyCipher.txt", "s")
Which of these will generate an error?
MyCipher.write("writing")
MyCipher..write(writing)
MyCipher.write("writing",writing)
MyCipher.readline()
Which file execute faster?
Text file
Binary file
CSV file
The default file - open mode is (a) mode.
How do you change the file position to an offset value from the start?
fp.seek(offset, 0)
fp.seek(offset, 1)
fp.seek(offset, 2)
none of the mentioned
4. What will be the ouput -
f=open('abc.txt','w')
x=f.read()
f.close()
print(x)
Error(not readable)
Error(not writable)
It will read all the data of the file.
None
What will be the output of the following Python code?
True
False
None
error
7. Name the module that has to be imported to write a csv file.
Pickle module
CSV module
csv module
Csv module
10. ______ is the extension of binary files
.txt
.bin
.dat
.Dat
To read 18 characters from the file object fobj , the correct syntax is ____________.
fobj.readlines(18)
fobj.readchar(18)
fobj.read(18)
fobj.readline(18)
Mention the data type of variable check in the following statements?
a. check=f.read()
b. check=f.readline()
c. check=f.readlines()
a. str b. str c. list
a. list b. str c. list
a. str b. list c. list
a. str b. list c. str
______________ is the process of converting Python object hierarchy into a byte stream so that it can be written to a file
Conversion
Translation
Serialization
unpickling
Raju created a binary file, student.dat file using the file handle fh. He wants to enter the details of the student into the student.dat file in the form of list. Help him to complete the code.
import ____________ # line 1
f=open('emp.dat','wb') # line 2
rec1=[101,'Sridhar','15 yrs',560] # line 3
rec2=[102,'Sujata','15 yrs',580] # line 4
_____________________________ # line 5
_____________________________ # line 6
f.close() # line 7
line 1: csv, line 5: f.write(rec1) , line 6: f.writerecord(rec2,f)
line 1: pickle, line 5: f.dump(rec1) , line 6: f.dump(rec2,f)
line 1: pickle, line 5: f,dump(rec1), line 6: f.dump(rec2)
line 1: pickle, line 5: pickle.dump(rec1,f) , line 6: pickle.dump(rec2,f)
The __________ function returns the current position of file pointer in the file.
tell()
loc
seek()
pos()
The __________ function changes the position of the file pointer by placing the file pointer at the specified position in an open file
tell()
loc
seek()
pos()
The default delimiter of the CSV file is ____________.
tab
comma
colon
filter
To create a csv file sales.csv, the correct syntax is _____________
f=open('sales.csv', 'w')
f=open('sales.csv', 'r')
f=open('sales.csv', 'w+')
f=open('sales.csv', 'a')
_________________ and __________ are the two objects associated with the csv module to reading and write onto a CSV file.
reader() and writer()
read() and write()
dump() and load()
write() and writelines()
_________________ function is used to write single row at a time.
writerow()
writerows()
writerecords()
writelines()
_________________ method writes all given rows to the csv file.
writerow()
writerows()
writeln()
writelines()
writeline()
To create the reader object, the correct syntax is __________.
[Assume that the salesdet.csv file is opened in reading mode in the file handle fh.
salesdet_reader=csv.reader(fh)
salesdet_reader=csvreader(fh)
salesdet_reader=csv.read(fh)
salesdet_reader=csvreader.fh
Which file is not binary file?
student.mp4
student.jpeg
student.csv
student.exe
