0% found this document useful (0 votes)
22 views3 pages

Python File Handling MCQs Guide

This document contains multiple-choice questions (MCQs) related to file handling in Python. It covers various aspects such as file modes, methods for reading and writing files, and assertions about file handling functions. The document serves as a study guide for understanding file operations in Python programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views3 pages

Python File Handling MCQs Guide

This document contains multiple-choice questions (MCQs) related to file handling in Python. It covers various aspects such as file modes, methods for reading and writing files, and assertions about file handling functions. The document serves as a study guide for understanding file operations in Python programming.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Department of Computer Science, PPUC UDUPI

Chapter-2
File Handling in Python(MCQ’s)
[Link] is the correct file mode to open a file for reading in text mode?
a) w c) rb
b) r d) r

2. Which of the following modes is used to open a file for writing in binary mode?
a) wb c) w
b) wt d) rb
Answer: a) wb

3. What will happen if you open a file that does not exist using the mode r?
a) File is created c) FileNotFoundError is raised
b) File is opened in write mode d) Nothing happens

4. Which method is used to read the entire content of a file as a string?


a) readline() c) readlines()
b) read() d) readfile()

5. What does the write() method do?


a) Reads from the file c) Writes data to the file
b) Appends data to the file d) Deletes the file

6. What is the purpose of with statement in file handling?


a) To import a module c) To ensure file is properly closed
b) To reduce the code d) To write multiple lines

7. Which method reads one line at a time from a file?


a) readlines() c) read()
b) readline() d) line()

8. Which of the following is used to open a file in append mode?


a) a c) r+
b) w+ d) x

9. What is the default mode when opening a file using open()?


a) w c) a
b) r d) rb

10. Which method returns all lines of a file as a list?


a) read() c) readlines()
b) readline() d) splitlines()

11. 1. Which of the following is used to open a file in Python?


a) openfile() c) open()
b) fileopen() d) readfile()

1
Department of Computer Science, PPUC UDUPI

12. What is the default mode in which a file is opened using the open() function?
a) w (write) c) r (read)
b) a (append) d) rb (read binary)

13. Which mode is used to open a file for writing, overwriting its content if it already exists?
a) a c) r
b) w d) x

14. What will happen if you try to read a file that does not exist?
a) It will create a new file c) It will raise a FileNotFoundError
b) It will write an empty string d) It will return None Answer: C

15. Which of the following is not a valid file mode in Python?


a) w+ c) r+
b) rw d) a+

16. Which function is used to read all lines of a text file into a list?
a) read() c) readlines()
b) readline() d) readall()

17. What does the tell() function return?


a) Total characters in the file c) Line number
b) Current file cursor position d) Size of file

18. What does the seek(0) do in a file?


a) Closes the file c) Moves the cursor to the beginning of
b) Opens the file in append mode the file
d) Skips the first line
19. Which method writes a list of strings to a file?
a) write() c) writelines()
b) writeline() d) print()

20. Which of the following statements is correct about closing a file in Python?
a) It is optional; Python always closes c) It is only required when writing files.
files automatically. d) close() is used only for binary files.
b) It is necessary to free system
resources.

[Link] (A): In Python, the `read()` function reads the entire file content as a single string.
Reason (R): The `readline()` function reads one line at a time from a file.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is true but R is false
d) A is false but R is true

22. Assertion (A): The `write()` function writes data into a file and automatically adds a newline at
the end.
Reason (R): The `write()` function needs `\n` explicitly to move to the next line.
a) Both A and R are true, and R is the correct explanation of A

2
Department of Computer Science, PPUC UDUPI

b) Both A and R are true, but R is not the correct explanation of A


c) A is false but R is true
d) Both A and R are false

23. Assertion (A): Binary files store data in human-readable format.


Reason (R): Binary files use binary encoding (0s and 1s) for storing data.
a) Both A and R are true, and R is the correct explanation of A
b) A is false but R is true
c) Both A and R are false
d) A is true but R is false Page 1

24. Assertion (A): Using `with open(filename)` automatically closes the file after its block is executed.
Reason (R): It is mandatory to call `close()` function after using `open()` with `with` statement.
a) Both A and R are true, and R is the correct explanation of A
b) Both A and R are true, but R is not the correct explanation of A
c) A is true but R is false
d) A is false but R is true

25. Assertion (A): The `seek()` function is used to move the file pointer to a specific position.
Reason (R): `tell()` function returns the current position of the file pointer.
a) Both A and R are true.
b) Both A and R are true, but R is not the correct explanation of A
c) A is true but R is false
d) A is false but R is true

******************************************

Common questions

Powered by AI

The 'read()' method reads the entire content of a file as a single string, while 'readline()' reads one line at a time from a file. These methods are used based on whether the entire file content or specific lines are needed for processing .

Using 'rb' mode opens a file for reading in binary format as opposed to text when using 'r'. This is significant when dealing with non-textual data, such as images or executable files, where preserving the exact byte sequence is necessary for correct data processing .

The 'writelines()' method writes a list of strings to a file without adding any newline characters between each string. In contrast, 'write()' writes a single string to the file and nothing else. If multiple lines are to be written with 'write()', newline characters must be manually included at the end of each string .

The 'with' statement in file handling is beneficial because it ensures that a file is properly closed after its suite is finished, even if an exception is raised. This is important for managing system resources efficiently and prevents resource leaks such as open file descriptors .

To open a file for writing in binary mode, the mode 'wb' should be used. If the file already exists, it will be overwritten and the existing data will be lost .

When attempting to open a file that does not exist using 'r' mode, a FileNotFoundError is raised because 'r' mode is intended for reading, and it is assumed the file is already present. Thus, if the file cannot be found, the operation fails .

Not using the 'close()' method after file operations can lead to resource leaks wherein the file remains open and occupies file descriptors, potentially leading to the exhaustion of resources especially in long-running applications or when many files are handled concurrently. It is essential to free system resources by closing files properly or use the 'with' statement which handles closure automatically .

The 'seek(0)' function moves the file cursor to the beginning of the file. This is useful when you need to re-read the file from the start after reading or writing operations have changed the cursor position .

The 'tell()' method returns the current position of the file cursor in the file, which helps in tracking how much data has been processed and serves as a reference point for further read or write operations .

The assertion states that binary files store data in human-readable format is false, whereas the reason correctly states that binary files use binary encoding (0s and 1s) for storing data. This means binary files are not directly readable by humans unless decoded properly, making the assertion false and the reason true .

You might also like