CBSE Class 12
Computer Science
02 File Handling in Python
30
Total
0
Attempted
0
Correct
0
Wrong
00:00:00
Q-1
What is the primary purpose of file handling in Python?
Easy
Introduction to Files
A
To store data permanently in files
B
To speed up program execution
C
To replace variables in memory
D
To convert Python code into binary
Answer: To store data permanently in files
Explanation: File handling allows programs to store and retrieve data from files for long-term storage.
Q-2
Which of the following is a type of file commonly used in Python?
Easy
Types of Files
A
Executable files
B
Text files
C
Compiler files
D
Interpreter files
Answer: Text files
Explanation: Text files are commonly used in Python to store human-readable data.
Q-3
Which mode is used to open a file for writing in Python?
Easy
Opening and Closing a Text File
A
r
B
a
C
w
D
x
Answer: w
Explanation: The 'w' mode opens a file for writing and creates it if it does not exist.
Q-4
Which function is used to open a file in Python?
Easy
Opening and Closing a Text File
A
read()
B
write()
C
file()
D
open()
Answer: open()
Explanation: The open() function is used to open a file in Python.
Q-5
What will be the output of the following code?
Easy
Writing to a Text File
Reference Code
```python
f = open("test.txt","w")
f.write("Hello")
f.close()
print("Done")
A
Done
B
Hello
C
test.txt
D
Error
Answer: Done
Explanation: The program writes 'Hello' to the file and prints 'Done'.
Q-6
Which method is used to write a string to a text file?
Easy
Writing to a Text File
A
read()
B
write()
C
append()
D
print()
Answer: write()
Explanation: The write() method writes text into a file.
Q-7
Which method reads the entire contents of a file?
Easy
Reading from a Text File
A
readline()
B
readlines()
C
read()
D
get()
Answer: read()
Explanation: The read() method reads the entire file content as a string.
Q-8
Which method reads one line at a time from a file?
Easy
Reading from a Text File
A
read()
B
getline()
C
readall()
D
readline()
Answer: readline()
Explanation: readline() reads a single line from a file.
Q-9
Which file mode opens a file for reading?
Easy
Opening and Closing a Text File
A
r
B
w
C
a
D
x
Answer: r
Explanation: Mode 'r' opens a file for reading.
Q-10
Which mode is used to append data to an existing file?
Easy
Writing to a Text File
A
w
B
a
C
r
D
x
Answer: a
Explanation: Mode 'a' appends data to the end of a file.
Q-11
What will be printed?
Medium
Writing to a Text File
Reference Code
```python
f=open("demo.txt","w")
f.write("ABC")
f.close()
A
ABC
B
Error
C
Nothing
D
demo.txt
Answer: Nothing
Explanation: The program writes to the file but does not print anything.
Q-12
Which function closes an opened file?
Easy
Opening and Closing a Text File
A
end()
B
stop()
C
finish()
D
close()
Answer: close()
Explanation: close() releases the file resource after operations.
Q-13
Which method returns all lines of a file as a list?
Medium
Reading from a Text File
A
readlines()
B
read()
C
getline()
D
lines()
Answer: readlines()
Explanation: readlines() returns a list containing each line of the file.
Q-14
Which function moves the file pointer to a specific position?
Medium
Setting Offsets in a File
A
tell()
B
seek()
C
move()
D
shift()
Answer: seek()
Explanation: seek() moves the file pointer to a specified position.
Q-15
What does the tell() function return?
Medium
Setting Offsets in a File
A
File size
B
Number of lines
C
Current file pointer position
D
File name
Answer: Current file pointer position
Explanation: tell() returns the current position of the file pointer.
Q-16
Which module is used to store Python objects in files?
Easy
The Pickle Module
A
json
B
csv
C
file
D
pickle
Answer: pickle
Explanation: The pickle module is used for serializing Python objects.
Q-17
Which function is used to write an object to a binary file using pickle?
Medium
The Pickle Module
A
dump()
B
load()
C
write()
D
store()
Answer: dump()
Explanation: pickle.dump() writes Python objects into a binary file.
Q-18
Which function reads a pickled object from a file?
Medium
The Pickle Module
A
read()
B
load()
C
open()
D
get()
Answer: load()
Explanation: pickle.load() retrieves serialized objects from a file.
Q-19
Which mode is used to open a binary file for writing?
Medium
Types of Files
A
rb
B
rt
C
wb
D
wt
Answer: wb
Explanation: wb mode opens a binary file for writing.
Q-20
Which loop is commonly used to traverse a text file?
Easy
Creating and Traversing a Text File
A
while loop
B
do loop
C
switch loop
D
for loop
Answer: for loop
Explanation: A for loop can iterate line by line through a file.
Q-21
Which mode creates a file but raises an error if it already exists?
Medium
Opening and Closing a Text File
A
x
B
w
C
a
D
r
Answer: x
Explanation: Mode 'x' creates a new file and throws an error if the file already exists.
Q-22
Which method reads all lines and returns them as a list?
Easy
Reading from a Text File
A
read()
B
readlines()
C
line()
D
fetch()
Answer: readlines()
Explanation: readlines() reads all lines and stores them in a list.
Q-23
What will be printed?
Easy
Writing to a Text File
Reference Code
```python
f=open("file.txt","w")
f.write("Python")
f.close()
print("File written")
A
Python
B
file.txt
C
File written
D
Nothing
Answer: File written
Explanation: The program writes to the file and prints 'File written'.
Q-24
Which of the following is a binary file example?
Easy
Types of Files
A
txt file
B
csv file
C
log file
D
image file
Answer: image file
Explanation: Image files store binary data.
Q-25
Which function tells the current position of the file pointer?
Easy
Setting Offsets in a File
A
tell()
B
seek()
C
read()
D
write()
Answer: tell()
Explanation: tell() returns the current file pointer position.
Q-26
Which function moves the pointer to another position in the file?
Easy
Setting Offsets in a File
A
tell()
B
seek()
C
read()
D
locate()
Answer: seek()
Explanation: seek() repositions the file pointer.
Q-27
Which module converts Python objects into byte streams?
Medium
The Pickle Module
A
json
B
csv
C
pickle
D
sys
Answer: pickle
Explanation: pickle serializes Python objects into byte streams.
Q-28
Which statement correctly opens a file for reading?
Easy
Opening and Closing a Text File
A
file("data.txt")
B
read("data.txt")
C
openfile("data.txt")
D
open("data.txt","r")
Answer: open("data.txt","r")
Explanation: open("data.txt","r") opens the file in read mode.
Q-29
Which structure is commonly used to traverse a file line by line?
Medium
Creating and Traversing a Text File
A
for line in file
B
switch
C
if condition
D
goto
Answer: for line in file
Explanation: Using 'for line in file' iterates through each line in the file.
Q-30
Which function is used to retrieve serialized data using pickle?
Medium
The Pickle Module
A
store()
B
load()
C
write()
D
append()
Answer: load()
Explanation: pickle.load() retrieves serialized objects from a file.
◀
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
▶