MCQs on File Handling | Python programming

1. Reading and Writing Files

  1. Which of the following is used to open a file in Python for reading?
    • a) open('file.txt', 'r')
    • b) open('file.txt', 'w')
    • c) open('file.txt', 'a')
    • d) open('file.txt', 'x')
  2. What method is used to read the entire content of a file in Python?
    • a) read()
    • b) readline()
    • c) readlines()
    • d) content()
  3. How do you write a string to a file in Python?
    • a) write()
    • b) writelines()
    • c) append()
    • d) put()
  4. What is the function used to open a file for writing in Python, creating it if it doesn’t exist?
    • a) open('file.txt', 'w')
    • b) open('file.txt', 'r')
    • c) open('file.txt', 'a')
    • d) open('file.txt', 'x')
  5. Which method reads one line from a file in Python?
    • a) readline()
    • b) read()
    • c) readlines()
    • d) next()

2. File Modes (Read, Write, Append)

  1. Which of the following file modes is used for appending data to an existing file?
    • a) 'r'
    • b) 'w'
    • c) 'a'
    • d) 'x'
  2. In which mode does Python open a file for both reading and writing, without truncating the file?
    • a) 'r'
    • b) 'w'
    • c) 'r+'
    • d) 'a+'
  3. Which of the following file modes will truncate the file if it already exists?
    • a) 'a'
    • b) 'r'
    • c) 'w'
    • d) 'x'
  4. What file mode should you use if you want to read and write to a file without truncating it?
    • a) 'r'
    • b) 'w+'
    • c) 'r+'
    • d) 'a'
  5. Which file mode is used to create a new file but fails if the file already exists?
    • a) 'r+'
    • b) 'a'
    • c) 'w'
    • d) 'x'

3. Working with Text Files and Binary Files

  1. To open a file in binary mode, which mode should be added to the standard read/write mode?
    • a) 'b'
    • b) 't'
    • c) 'x'
    • d) 'bin'
  2. Which of the following methods can be used to read a binary file?
    • a) read()
    • b) readline()
    • c) readlines()
    • d) readbin()
  3. Which method is used to write data in binary mode to a file?
    • a) write()
    • b) writelines()
    • c) writebin()
    • d) wb()
  4. Which of the following can be used to open a file in text mode for writing?
    • a) 'wb'
    • b) 'ab'
    • c) 'wt'
    • d) 'w'
  5. When working with binary files, which of the following is correct for reading data from a binary file?
    • a) f.read()
    • b) f.readlines()
    • c) f.read(10)
    • d) All of the above

4. File Object Methods

  1. Which method is used to close a file after performing file operations?
    • a) close()
    • b) exit()
    • c) stop()
    • d) end()
  2. How do you read a specific number of bytes from a file?
    • a) read(num_bytes)
    • b) readline(num_bytes)
    • c) readlines(num_bytes)
    • d) seek(num_bytes)
  3. Which of the following methods will return the current position of the file pointer?
    • a) tell()
    • b) seek()
    • c) position()
    • d) getpos()
  4. How do you move the file pointer to a specific position in the file?
    • a) seek(position)
    • b) tell(position)
    • c) goto(position)
    • d) move(position)
  5. Which method is used to return a list of lines from a file?
    • a) readlines()
    • b) read()
    • c) readline()
    • d) content()

5. Exception Handling in File Operations

  1. What is the exception raised when trying to open a non-existent file in read mode?
    • a) FileNotFoundError
    • b) IOException
    • c) PermissionError
    • d) ValueError
  2. Which of the following statements is correct for handling file-related exceptions in Python?
    • a) try-except
    • b) try-finally
    • c) try-except-finally
    • d) All of the above
  3. What happens if you attempt to read from a file that has been closed?
    • a) It raises a FileNotFoundError
    • b) It raises a ValueError
    • c) It raises an IOError
    • d) It raises an AttributeError
  4. Which exception is raised when there is an issue with reading or writing a file due to incorrect file permissions?
    • a) FileNotFoundError
    • b) PermissionError
    • c) OSError
    • d) ValueError
  5. How do you handle an exception that occurs while performing file operations in Python?
    • a) Use try-except block
    • b) Use finally block
    • c) Use with statement
    • d) Use open() method

6. Context Managers (with statement)

  1. What is the primary benefit of using a with statement when working with files?
    • a) It automatically closes the file
    • b) It helps in error handling
    • c) It prevents file corruption
    • d) It automatically opens the file
  2. What does the with open('file.txt', 'r') as f: syntax do in Python?
    • a) Opens the file and automatically closes it after use
    • b) Opens the file for writing
    • c) Creates a new file if it doesn’t exist
    • d) Opens the file for reading and writing
  3. Which statement is true about the with statement in Python?
    • a) It automatically closes the file even if an exception is raised
    • b) It does not close the file automatically
    • c) It raises an exception when file operations are completed
    • d) It is only used for file reading
  4. In a with statement, what happens if an exception occurs within the block?
    • a) The exception is automatically handled and the file is closed
    • b) The exception is ignored
    • c) The file is not closed
    • d) The program crashes without handling the exception
  5. Which of the following is correct for reading a file using a context manager?
    • a) with open('file.txt', 'r') as file:
    • b) open('file.txt', 'r') as file:
    • c) open('file.txt')
    • d) with open() as file:

Answers (Tabular Form)

Question No.Answer
1a
2a
3a
4a
5a
6c
7c
8c
9c
10d
11a
12a
13a
14d
15d
16a
17a
18a
19a
20a
21a
22d
23c
24b
25a
26a
27a
28a
29a
30a

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top