Search Tutorials


Python File Handling MCQ Questions and Answers | JavaInUse

Python File Handling MCQ Questions and Answers

Q. What is the mode in which a file is opened for both reading and writing?

A. 'r'
B. 'w'
C. 'r+'
D. 'a'

Q. What is the correct way to open a file named "data.txt" in append mode?

A. file = open("data.txt", "a")
B. file = open("data.txt", "w")
C. file = open("data.txt", "r+")
D. file = open("data.txt", "a+")

Q. What is the correct way to read the entire content of a file at once?

A. file.read()
B. file.readlines()
C. file.readall()
D. file.readlinesall()

Q. What is the correct way to write a string to a file?

A. file.write("Hello, World!")
B. file.put("Hello, World!")
C. file.append("Hello, World!")
D. file.add("Hello, World!")

Q. What is the correct way to close a file?

A. file.close()
B. file.open(mode="closed")
C. file.end()
D. file = None

Q. What is the purpose of the "with" statement when working with files?

A. To open a file in a specific mode
B. To ensure the file is properly closed after usage
C. To create a new file
D. To delete a file

Q. What is the correct way to read lines from a file and store them in a list?

A. lines = file.readlines()
B. lines = file.readlist()
C. lines = file.readlineslist()
D. lines = file.readall()

Q. How can you check if a file exists before attempting to open it?

A. file.exists()
B. os.path.exists(file)
C. file.is_file()
D. file.isfile()

Q. What is the purpose of the "tell()" method in file handling?

A. To get the current position in the file
B. To set the position in the file
C. To get the size of the file
D. To check if the file is empty

Q. What is the purpose of the "seek()" method in file handling?

A. To set the position in the file
B. To get the current position in the file
C. To get the size of the file
D. To check if the file is empty

Q. What is the correct way to rename a file?

A. os.rename("old_file.txt", "new_file.txt")
B. file.rename("old_file.txt", "new_file.txt")
C. file.move("old_file.txt", "new_file.txt")
D. os.move("old_file.txt", "new_file.txt")

Q. What is the correct way to delete a file?

A. os.remove("file.txt")
B. file.remove("file.txt")
C. file.delete("file.txt")
D. os.delete("file.txt")





Q. What is the purpose of the "flush()" method in file handling?

A. To clear the file buffer
B. To set the file buffer size
C. To get the size of the file buffer
D. To check if the file buffer is empty

Q. What is the purpose of the "truncate()" method in file handling?

A. To truncate the file to a specified size
B. To get the current size of the file
C. To set the file size to zero
D. To check if the file size is greater than a given value

Q. What is the purpose of the "fileno()" method in file handling?

A. To get the file descriptor
B. To set the file descriptor
C. To get the file size
D. To check if the file is open

Q. What will be the output of the following code snippet?

file = open("test.txt", "w")
file.write("Hello, World!")
file.close()
    
A.
The code will create a new file named "test.txt" and write "Hello, World!" to it.
B.
The code will overwrite the content of "test.txt" with "Hello, World!" if it already exists.
C.
An error will occur because the file mode is not specified as "text" or "binary".
D.
The code will append "Hello, World!" to the content of "test.txt" if it already exists.

Q. Which of the following code snippets demonstrates the correct way to read the content of a file line by line?

A.
file = open("test.txt", "r")
content = file.read()
file.close()

for line in content:
    print(line)
    
B.
with open("test.txt", "r") as file:
    for line in file:
        print(line)
    
C.
file = open("test.txt", "r")

while True:
    line = file.readline()
    if not line:
        break
    print(line)
    
file.close()
    
D.
file = open("test.txt", "r")
content = file.readlines()
file.close()

for line in content:
    print(line)
    

Q. Which of the following code snippets demonstrates the correct way to append content to an existing file?

A.
file = open("test.txt", "a")
file.write("This is a new line.")
file.close()
    
B.
with open("test.txt", "a") as file:
    file.write("This is a new line.")
    
C.
file = open("test.txt", "w")
file.write("This is a new line.")
file.close()
    
D.
file = open("test.txt", "r")
file.write("This is a new line.")
file.close()
    

Q. Which of the following code snippets demonstrates the correct way to rename a file?

A.
os.rename("old_file.txt", "new_file.txt")
    
B.
os.replace("old_file.txt", "new_file.txt")
    
C.
os.move("old_file.txt", "new_file.txt")
    
D.
os.change_name("old_file.txt", "new_file.txt")
    

Q. What will be the output of the following code snippet?

file = open("test.txt", "r")
content = file.read()
file.close()

print(content.strip())
    
Assume that "test.txt" contains the following content:
     Hello, World! 
    
A.
     Hello, World!
    
B.
    Hello, World! 
    
C.
Hello, World!
    
D.
An error will occur because strip() is not a valid method for strings.

Q. Which of the following code snippets demonstrates the correct way to delete a file?

A.
os.remove("test.txt")
    
B.
os.delete("test.txt")
    
C.
os.remove_file("test.txt")
    
D.
os.delete_file("test.txt")
    

Q. What will be the output of the following code snippet?

file = open("test.csv", "r")
content = file.read()
file.close()

print(content.split(","))
    
Assume that "test.csv" contains the following content:
Name,Age,City
Alice,25,New York
Bob,30,Los Angeles
    
A.
['Name', ' Age', 'City', 'Alice', '25', 'New York', 'Bob', '30', 'Los Angeles']
    
B.
['Name', 'Age', 'City', 'Alice,25,New York', 'Bob,30,Los Angeles']
    
C.
['Name', 'Age', 'City']
['Alice', '25', 'New York']
['Bob', '30', 'Los Angeles']
    
D.
An error will occur because split() cannot take a string as an argument.

Q. Which of the following code snippets demonstrates the correct way to write a list of dictionaries to a CSV file?

A.
data = [{"Name": "Alice", "Age": 25}, {"Name": "Bob", "Age": 30}]

with open("output.csv", "w") as file:
    for item in data:
        file.write(",".join(item.values()))
        file.write("\n")
    
B.
import csv

data = [{"Name": "Alice", "Age": 25}, {"Name": "Bob", "Age": 30}]

with open("output.csv", "w", newline="") as file:
    writer = csv.writer(file)
    writer.writerows(data)
    
C.
data = [{"Name": "Alice", "Age": 25}, {"Name": "Bob", "Age": 30}]

with open("output.csv", "w") as file:
    writer = csv.DictWriter(file, fieldnames=data[0].keys())
    writer.writeheader()
    writer.writerows(data)
    
D.
data = [{"Name": "Alice", "Age": 25}, {"Name": "Bob", "Age": 30}]

with open("output.csv", "w") as file:
    for item in data:
        for key, value in item.items():
            file.write(f"{key}:{value},")
        file.write("\n")