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
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
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
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
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
f = open("test.txt","w")
f.write("Hello")
f.close()
print("Done")
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
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
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
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
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
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
f=open("demo.txt","w")
f.write("ABC")
f.close()
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
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
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
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
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
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
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
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
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
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
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
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
f=open("file.txt","w")
f.write("Python")
f.close()
print("File written")
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
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
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
Answer: seek()
Explanation: seek() repositions the file pointer.
Q-27
Which module converts Python objects into byte streams?
Medium
The Pickle Module
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
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
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
Answer: load()
Explanation: pickle.load() retrieves serialized objects from a file.