Ques 1 File Handling
What is the purpose of the open() function in Python?
a) To create a new file
b) To read the contents of a file
c) To write data to a file
d) All of the above
Ques 2 File Handling
Which mode is used to open a file for writing in Python?
a) 'r'
b) 'w'
c) 'a'
d) 'x'
Ques 3 File Handling
What will be the output of the following Python code snippet?
file = open("filename.txt", "a") file.write("This is an appended line.") file.close()
Ques 4 File Handling
What will be the output of the following Python code snippet?
file = open("filename.txt", "r") content = file.read() file.close() print(content)
Ques 5 File Handling
What does the 'with' statement do in file handling?
a) Ensures proper file closure even if an exception occurs
b) Renames a file
c) Deletes the contents of a file
d) Opens a file for writing
Ques 6 File Handling
What will be the output of the following Python code snippet?
file = open("filename.txt", "w") file.write("Hello, World!") file.close()