Python MCQ on File Handling Exceptions. We covered all the Python MCQ on File Handling Exceptions in this post for free so that you can practice well for the exam.
Install our MCQTUBE Android app from the Google Play Store and prepare for any competitive government exams for free.
We created all the competitive exam MCQs into several small posts on our website for your convenience.
You will get their respective links in the related posts section provided below.
Related Posts:
- Python Functions Multiple Choice Questions with Answers
- Python Functions MCQ For Interview Preparation
- Python Interview Questions MCQ on Core Concepts
Python MCQ on File Handling Exceptions for Students
Why is file handling used in Python?
a. To generate new files
b. To access and modify file content
c. To display output on the screen
d. To carry out calculations
Option b – To access and modify file content
In what type of characters is data saved in a text file?
a. ASCII
b. Unicode
c. Both a and b
d. Neither a nor b
Option c – Both a and b
Which Python method is used to delete a file from the system?
a. remove()
b. delete()
c. erase()
d. destroy()
Option a – remove()
What is the function of the seek() method in Python file operations?
a. It creates a new file.
b. It shifts the file pointer to a specified position.
c. It closes an open file.
d. It saves data to a file.
Option b – It shifts the file pointer to a specified position.
Which of the following is an invalid file access mode in Python?
a. “x”
b. “a+”
c. “rw”
d. “rb”
Option c – “rw”
What file mode should be used to open a binary file for reading?
a. “r”
b. “w”
c. “rb”
d. “wb”
Option c – “rb”
What is the function of the readlines() method in Python?
a. Retrieves a single line from the file.
b. Reads the full content of the file as one string.
c. Loads all lines from the file into a list.
d. Extracts only the first few lines of the file.
Option c – Loads all lines from the file into a list.
How can you loop through every line in a file using Python?
a. Use the for line in file:
syntax.
b. Use the for line in file.read():
syntax.
c. Use the for line in file.readlines():
syntax.
d. Use the for line in file.readline():
syntax.
Option a – Use the for line in file: syntax.
Which method allows writing a list of strings into a text file in Python?
a. write()
b. writelines()
c. writeall()
d. writestring()
Option b – writelines()
Which Python function helps in creating a new folder?
a. make_dir()
b. create_directory()
c. mkdir()
d. newdir()
Option c – mkdir()
What does the flush() function do in file handling?
a. Closes the open file.
b. Forces unwritten data to be saved to the file.
c. Deletes the contents of the file.
d. Loads data from the file into memory.
Option b – Forces unwritten data to be saved to the file.
How do you open a binary file for writing, ensuring the file is created if it doesn’t exist?
a. file = open("file.bin", "w")
b. file = open("file.bin", "wb")
c. file = open("file.bin", "a")
d. file = open("file.bin", "r")
Option b – file = open(“file.bin”, “wb”)
What is a typical method to read the first three lines of a file into a list?
a. readlines()[:3]
b. readline(3)
c. readlines(3)
d. readline()[:3]
Option a – readlines()[:3]
How do you detect if the end of a file has been reached in Python?
a. Use file.at_end()
.
b. Use file.end()
.
c. Check if file.read()
returns an empty string.
d. Use file.eof()
.
Option c – Check if file.read() returns an empty string.
Which method resets the file pointer to the start of the file?
a. reset()
b. seek(0)
c. move(0)
d. position(0)
Option b – seek(0)
Why is the os
module important for file operations in Python?
a. It supports file creation and opening.
b. It helps manage file paths and directories.
c. It handles reading and writing of files.
d. It compresses and decompresses file data.
Option b – It helps manage file paths and directories.
What function would you use to change the name of a file in Python?
a. rename_file()
b. move_file()
c. os.rename()
d. change_file_name()
Option c – os.rename()
Which method allows you to view all files and folders within a specific directory using Python?
a. Apply the listdir() function from the os module.
b. Call the files_and_directories() method.
c. Use the ls() command in Python.
d. Utilize os.listdir() for accessing contents.
Option d – Utilize os.listdir() for accessing contents.
In Python, what action does the os.remove() function perform?
a. It creates a brand new file.
b. It transfers a file to a different directory.
c. It deletes a file from the storage.
d. It changes the name of a file.
Option c – It deletes a file from the storage.
Why would you use the os.path.getatime() function in Python?
a. To know the file’s storage size in bytes.
b. To retrieve the last time the file was accessed, in seconds since epoch.
c. To verify whether a path refers to a folder.
d. To get the timestamp of the last file update.
Option b – To retrieve the last time the file was accessed, in seconds since epoch.
Which function helps confirm that a specific path both exists and is a directory in Python?
a. os.path.is_directory()
b. os.path.exists()
c. os.path.isfile()
d. os.path.isdir()
Option d – os.path.isdir()
What does os.path.getmtime() return when used on a file?
a. The time when the file was created.
b. The timestamp for the file’s most recent edit.
c. Whether a path is relative or absolute.
d. The size of the file in bytes.
Option b – The timestamp for the file’s most recent edit.
How can you make a directory in Python, including any required parent folders?
a. Apply os.mkdir().
b. Use os.makedirs() and include the exist_ok=True flag.
c. Run the create_directory() function.
d. Call the newdir() command.
Option b – Use os.makedirs() and include the exist_ok=True flag.
What is retrieved by using os.path.getctime() in Python?
a. File size in bytes.
b. The timestamp when the file was last changed.
c. The time the file was initially created.
d. A check for whether the path leads to a directory.
Option c – The time the file was initially created.
What function lets you rename a folder in Python?
a. rename_directory()
b. move_directory()
c. os.rename()
d. change_directory_name()
Option c – os.rename()
How do you determine if a certain path in Python points to a symbolic link?
a. os.path.issymlink()
b. os.path.islink()
c. is_symbolic_link()
d. file.is_symlink()
Option b – os.path.islink()
Why is the os.path.getsize() method used in Python?
a. To find out how large a file is in bytes.
b. To return a file’s full path.
c. To access the time of the last modification.
d. To test if a path is a folder.
Option a – To find out how large a file is in bytes.
Which mode allows both reading and writing to a file in Python, while creating it if it’s not there?
a. “r”
b. “w”
c. “a”
d. “a+”
Option d – “a+”
If a path doesn’t lead to a folder, what does os.path.isdir() return?
a. True
b. False
c. None
d. It throws an error
Option b – False
Why would someone use the os.path.realpath() function in Python?
a. To check whether a given path is relative or absolute.
b. To obtain the complete absolute path of a file.
c. To convert a relative path into an absolute one.
d. To resolve and return the true system path of a file.
Option d – To resolve and return the true system path of a file.
What happens when you open a file in Python using the “a” mode?
a. New data is added to the end of the file.
b. The file is opened for reading only.
c. Existing content is deleted and replaced.
d. A completely new file is created.
Option a – New data is added to the end of the file.
How can multiple lines be written to a file using Python’s writelines()
function?
a. Use a loop to call write()
on each line individually.
b. Pass a list of lines to the writelines()
function.
c. Use the writelinen()
function.
d. Use a method called writemultiple()
.
Option b – Pass a list of lines to the writelines() function.
What does Python’s read()
function do when reading from a file?
a. Retrieves one line from the file.
b. Loads the entire file content into a single string.
c. Extracts a specified number of characters.
d. Reads the file into a list of lines.
Option b – Loads the entire file content into a single string.
How can you read only a certain number of characters from a file in Python?
a. Use readline(n)
.
b. Use read(n)
.
c. Use readlines(n)
.
d. Use readto(n)
.
Option b – Use read(n).
Is it necessary to manually close a file when using the with
statement in Python?
a. Yes, you need to close it yourself.
b. No, it closes automatically after the block finishes.
c. Depends on your Python version.
d. Only necessary if an error happens.
Option b – No, it closes automatically after the block finishes.
What is the role of the readline()
function in file operations?
a. It reads the entire file in one go.
b. It retrieves the first line of the file.
c. It reads a specific line from anywhere in the file.
d. It accesses the last line of the file.
Option b – It retrieves the first line of the file.
What’s the correct way to get all lines from a file into a list in Python?
a. Use a loop with readline()
.
b. Use the readlines()
method.
c. Use readall()
.
d. Use readline(n)
.
Option b – Use the readlines() method.
What is the use of the seek()
function in Python’s file handling?
a. To make a new file.
b. To move the file pointer to a particular spot.
c. To close the file.
d. To write content into the file.
Option b – To move the file pointer to a particular spot.
How can you store all lines from a file into a Python list?
a. Read the whole file using read()
.
b. Use readline()
inside a loop.
c. Call readall()
.
d. Use the readlines()
function.
Option d – Use the readlines() function.
What does the tell()
function return in Python file operations?
a. Total size of the file.
b. The current position of the file pointer.
c. Number of lines in the file.
d. Last modified time of the file.
Option b – The current position of the file pointer.
What effect does opening a file in “w” mode have on existing content?
a. Keeps the old data.
b. Adds new data to the end.
c. Deletes old content and writes new.
d. Reads and returns existing content.
Option c – Deletes old content and writes new.
What does os.path.getsize()
return in Python?
a. The file’s size in bytes.
b. Time of last access since epoch.
c. Verifies if a path points to a directory.
d. Provides the file’s extension.
Option a – The file’s size in bytes.
How can you add data to a file in Python without erasing its existing contents?
a. Open the file using “r+” mode
b. Use the write() method along with “a” mode
c. Open the file with “a+” mode
d. Use an append() function
Option b – Use the write() method along with “a” mode
What does the tell() function provide when working with files in Python?
a. The total number of lines in the file
b. The file’s size measured in bytes
c. The current position of the cursor in the file
d. The time when the file was last modified
Option c – The current position of the cursor in the file
What is the way to read a particular line from a file in Python?
a. Pass an index to the readline() method
b. Call the readlines() method with a specific line number
c. Use readline(n) where n is the line number
d. Iterate through lines with readline() in a loop
Option c – Use readline(n) where n is the line number
What happens when you open a file with the “rb” mode in Python?
a. The file is opened for reading in binary format
b. The file is opened for writing in binary format
c. The file’s content is read as binary data
d. The file’s content is read as text data
Option a – The file is opened for reading in binary format
How can you read a fixed number of lines from a file in Python?
a. Call readline() with the number of lines as argument
b. Loop over readlines() with a limit on the count
c. Use readline(n) where n specifies the number of lines
d. Manually count lines using readline() in a loop
Option b – Loop over readlines() with a limit on the count
What is the function of the flush() method when used with a file in Python?
a. It closes the open file
b. It forces the buffer to write its content to the storage
c. It clears the content inside the file
d. It reads the data from the file
Option b – It forces the buffer to write its content to the storage
How can you verify if a file exists before accessing it in Python?
a. Call os.file_exists() function
b. Use os.path.exists() function
c. Use file.exists() method
d. Use file.check_exists() method
Option b – Use os.path.exists() function
If you use open() without specifying the mode, what is the default mode for reading in Python?
a. An empty string “”
b. Write mode “w”
c. Append mode “a”
d. Read mode “r”
Option d – Read mode “r”
Which method is designed to write data into a file in Python?
a. read()
b. readline()
c. writedata()
d. write()
Option d – write()
How do you read all lines from a file into a list while keeping newline characters?
a. Use read() method
b. Use readline() repeatedly in a loop
c. Use readlines() method
d. Use readall() method
Option c – Use readlines() method
Where is the file cursor positioned when a file is opened in read mode by default?
a. At the start of the file (position 0)
b. At the end of the file
c. In the middle of the file
d. Depends on the file’s contents
Option a – At the start of the file (position 0)
How can you move the cursor to the end of a file in Python?
a. Use file.seek(0)
b. Use file.seek(1)
c. Use file.seek(0, 2)
d. Use file.end()
Option c – Use file.seek(0, 2)
When you open a file in Python for reading, where is the file pointer positioned by default?
a. At the file’s end
b. Somewhere in the middle
c. At the very beginning (position 0)
d. At a random spot
Option c – At the very beginning (position 0)
What is the correct way to set the file pointer to a particular character position in a text file using Python?
a. Call the file.character() method
b. Use file.seek(0, character)
c. Use file.seek(character)
d. Use file.position(character)
Option c – Use file.seek(character)
Which method allows you to read a file one line at a time in Python?
a. read(1)
b. readlines(1)
c. readline()
d. line()
Option c – readline()
When opening a file in append mode, where does the file pointer initially point?
a. Start of the file (position 0)
b. End of the file
c. Middle of the file
d. Random location
Option b – End of the file
What is the main function of the seek() method when working with files in Python?
a. Moves the pointer to the file’s end
b. Closes the file
c. Writes data into the file
d. Changes the pointer’s position to a specified spot
Option d – Changes the pointer’s position to a specified spot
If you use the tell() method after closing a file, what value does it return?
a. 0
b. 1
c. -1
d. None
Option c – -1
How can you reset the file pointer back to the start of a file in Python?
a. Call file.seek(0)
b. Call file.seek(1)
c. Call file.seek(0, 2)
d. Call file.begin()
Option a – Call file.seek(0)
How would you move the file pointer to a particular line number in a text file using Python?
a. Use file.line()
b. Use file.seek(0, line)
c. Use file.seek(line)
d. Use file.position(line)
Option c – Use file.seek(line)
What does the seek() method return if it fails to reposition the file pointer in Python?
a. The updated file position
b. False
c. None
d. An error message
Option c – None
Which method moves the file pointer to the file’s end in Python?
a. file.seek(0)
b. file.seek(1)
c. file.seek(0, 2)
d. file.end()
Option c – file.seek(0, 2)
What value does tell() return if you call it after the file is closed?
a. 0
b. 1
c. -1
d. None
Option c – -1
Which Python module is typically used for file operations such as renaming or deleting files?
a. os
b. shutil
c. sys
d. pathlib
Option b – shutil
How do you rename a file using the shutil module in Python?
a. shutil.rename(“old_file.txt”, “new_file.txt”)
b. shutil.move(“old_file.txt”, “new_file.txt”)
c. shutil.copy(“old_file.txt”, “new_file.txt”)
d. shutil.rename_file(“old_file.txt”, “new_file.txt”)
Option b – shutil.move(“old_file.txt”, “new_file.txt”)
Which function in the shutil module is designed to rename files?
a. move()
b. rename()
c. copy()
d. replace()
Option b – rename()
We covered all the Python MCQ on File Handling Exceptions above in this post for free so that you can practice well for the exam.
Check out the latest MCQ content by visiting our mcqtube website homepage.
Also, check out:
- Simple Python Basic Quiz Questions and Answers
- Basic Programming MCQ for Beginners with Answers
- Computer MCQ for Class 10 with Answers PDF