File handling in python, Study Guides, Projects, Research of Computer science

File handling in python with detailed explanation and examples

Typology: Study Guides, Projects, Research

2025/2026

Available from 01/24/2026

abhishek-voi
abhishek-voi 🇮🇳

5 documents

1 / 10

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MULTIPLE CHOICE QUESTIONS (MCQ)
1.
Introduction to Files
1.
What is the primary purpose of storing data in files?
a)
To execute programs faster
b)
To permanently store data for later access
c)
To reduce memory usage
d)
To improve program readability
2.
Where are files stored in a computer system?
a)
Primary memory (RAM)
b)
Secondary storage media
c)
CPU registers
d)
Cache memory
Types of Files
2.
Which of the following is a text file extension?
a)
.txt
b)
.jpg
c)
.exe
d)
.mp3
3.
What is the main difference between text and binary files?
a)
Text files are smaller in size
b)
Binary files contain human-readable characters
c)
Text files consist of ASCII/Unicode characters
d)
Binary files cannot be opened in Python
4.
What happens if a binary file is opened in a text editor?
a)
It displays the correct content
b)
It shows garbage values
c)
It automatically converts to text
d)
It asks for a password
5.
Which of the following is an example of a binary file?
a)
A .csv file
b)
A .py file
c)
A .docx file
d)
A .txt file
6.
What is the default EOL character in Python?
a)
\r
b)
\n
c)
\t
d)
\0
7.
Which encoding scheme is commonly used for text files?
a)
ASCII
b)
JPEG
c)
MPEG
d)
ZIP
3.
Opening
and
Closing
Files
8.
Which function is used to open a file in Python?
a)
file_open()
b)
open()
c)
load()
d)
read()
9.
What is the default mode for opening a file?
a)
Read mode (‘r’)
b)
Write mode (‘w’)
c)
Append mode (‘a’)
d)
Binary mode (‘b’)
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download File handling in python and more Study Guides, Projects, Research Computer science in PDF only on Docsity!

MULTIPLE CHOICE QUESTIONS (MCQ)

  1. Introduction to Files
    1. What is the primary purpose of storing data in files? a) To execute programs faster b) To permanently store data for later access c) To reduce memory usage d) To improve program readability
    2. Where are files stored in a computer system? a) Primary memory (RAM) b) Secondary storage media c) CPU registers d) Cache memory Types of Files
    3. Which of the following is a text file extension? a) .txt b) .jpg c) .exe d) .mp
    4. What is the main difference between text and binary files? a) Text files are smaller in size b) Binary files contain human-readable characters c) Text files consist of ASCII/Unicode characters d) Binary files cannot be opened in Python
    5. What happens if a binary file is opened in a text editor? a) It displays the correct content b) It shows garbage values c) It automatically converts to text d) It asks for a password
    6. Which of the following is an example of a binary file? a) A .csv file b) A .py file c) A .docx file d) A .txt file
    7. What is the default EOL character in Python? a) \r b) \n c) \t d) \ 0
    8. Which encoding scheme is commonly used for text files? a) ASCII b) JPEG c) MPEG d) ZIP
  2. Opening and Closing Files
    1. Which function is used to open a file in Python? a) file_open() b) open() c) load() d) read()
  3. What is the default mode for opening a file? a) Read mode (‘r’) b) Write mode (‘w’) c) Append mode (‘a’) d) Binary mode (‘b’)
  1. Which mode is used to open a file for both reading and writing? a) ‘r’ b) ‘w+’ c) ‘a’ d) ‘b’
  2. What happens if a file opened in ‘w’ mode already exists? a) The file is opened in read mode b) The existing content is overwritten c) The file is deleted d) An error occurs
  3. Which attribute returns the access mode of a file? a) file.name b) file.mode c) file.closed d) file.size
  4. How is a file closed in Python? a) file.close() b) close(file) c) file.exit() d) exit(file)
  5. What is the advantage of using the with clause to open a file? a) It allows faster file operations b) It automatically closes the file c) It encrypts the file d) It compresses the file
  6. Writing to a Text File
  7. Which method is used to write a single string to a file? a) writeline() b) write() c) append() d) insert()
  8. What does the write() method return? a) The number of characters written b) The file object c) The content of the file d) None
  9. How can numeric data be written to a text file? a) Directly using write() b) By converting it to a string first c) Using the dump() method d) It cannot be written
  10. Which method is used to write multiple strings to a file? a) writelines() b) write() c) appendlines() d) insertlines()
  11. What is the purpose of the flush() method? a) To close the file b) To clear the buffer and write contents to the file c) To read the file d) To delete the file
  1. Creating and Traversing a Text File
    1. What happens if a file opened in ‘a’ mode does not exist? a) An error occurs b) A new file is created c) The file is deleted d) The file is opened in read mode
    2. Which mode is used to open a file for both reading and writing without overwriting existing content? a) ‘r+’ b) ‘w+’ c) ‘a+’ d) ‘b+’
    3. How can you iterate over all lines in a file? a) Using a for loop on the file object b) Using a while loop with readline() c) Both a and b d) Using seek()
    4. What is the output of fileobject.tell() after writing data to a file? a) The number of lines written b) The current byte position of the file object c) The file size d) The number of characters written
  2. The Pickle Module
    1. What is the purpose of the pickle module? a) To read text files b) To serialize and deserialize Python objects c) To write binary files d) To compress files
    2. Which method is used to write Python objects to a binary file? a) write() b) dump() c) load() d) pickle()
    3. Which method is used to read Python objects from a binary file? a) read() b) load() c) get() d) unpickle()
    4. What mode is used to open a binary file for writing? a) ‘w’ b) ‘wb’ c) ‘wr’ d) ‘w+’
    5. What happens if a binary file is corrupted? a) It can be easily fixed b) It becomes unreadable c) It automatically repairs itself d) It converts to a text file
    6. Which exception is raised when the end of a binary file is reached during unpickling? a) FileNotFoundError b) EOFError c) IOError d) ValueError

Miscellaneous

  1. What is serialization? a) Converting Python objects to byte streams b) Reading text files c) Writing binary files d) Closing files
  2. What is deserialization? a) Converting byte streams to Python objects b) Writing text files c) Reading binary files d) Opening files
  3. Which module is required for pickling and unpickling? a) io b) pickle c) os d) sys
  4. What is the output of file.closed if the file is open? a) True b) False c) 1 d) 0
  5. Which method is used to forcefully write buffer contents to a file? a) close() b) flush() c) write() d) dump()
  6. What is the purpose of the splitlines() method? a) To split a file into multiple files b) To split a string into a list of lines c) To join lines in a file d) To close a file
  7. Which of the following is not a file attribute? a) file.name b) file.mode c) file.size d) file.closed
  8. What is the correct way to open a file for reading and writing in binary mode? a) open(“file.dat”, “r+b”) b) open(“file.dat”, “rwb”) c) open(“file.dat”, “br+”) d) open(“file.dat”, “b+r”)
  9. What is the correct syntax for the with clause? a) with open(“file.txt”, “r”) as f: b) with open(“file.txt”, “r”) - > f: c) with open(“file.txt”, “r”) in f: d) with open(“file.txt”, “r”) f:
  10. What is the purpose of the io module in Python? a) To handle file operations b) To perform mathematical calculations c) To create graphical interfaces d) To connect to databases
  11. Assertion (A): A text file can be opened and read using a text editor. Reason (R): Text files store data in the form of ASCII or Unicode characters. Options: (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

(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

  1. Assertion (A): The read() method reads the complete file when no parameter is passed. Reason (R): It returns a list of strings when the file is completely read. Options: (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
  2. Assertion (A): The readline() method reads the file until a newline character is found. Reason (R): It always reads the complete file in one go. Options: (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
  3. Assertion (A): The readlines() method returns a list containing each line as an element. Reason (R): This method is useful when we want to iterate through lines as list elements. Options: (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 AA is true, but R is false (C) A is false, but R is true
  4. Assertion (A): The tell() function returns the current byte position of the file object. Reason (R): It is helpful when tracking file offset during file operations. Options: (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
  5. Assertion (A): The seek() method is used to randomly access a file. Reason (R): It always moves the file pointer to the beginning of the file. Options: (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
  6. Assertion (A): The with statement is preferred in Python for file handling. Reason (R): It automatically closes the file when execution goes out of scope.

Options: (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

  1. Assertion (A): Pickling converts a Python object into a byte stream. Reason (R): It is useful for storing Python objects in binary files. Options: (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
  2. Assertion (A): The dump() method writes binary data to a file. Reason (R): The file must be opened in 'rb' mode to use dump(). Options: (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
  3. Assertion (A): The load() method is used for unpickling data from a binary file. Reason (R): It recreates the original Python object from the byte stream. Options: (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
  4. Assertion (A): All Python objects can be pickled. Reason (R): The pickle module can serialize even open file objects and sockets. Options: (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 (Not all objects can be pickled – open files, sockets, etc., cannot be.) Assertion (A): A file opened using mode 'a+' allows both appending and reading. Reason (R): The file pointer is initially placed at the beginning of the file. Options: (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 (In 'a+', pointer is at the end , not beginning.)
  5. Assertion (A): If a file opened using 'w' mode does not exist, it will be created. Reason (R): The 'w' mode only works on already existing files.
  1. The method splits strings at whitespace by default.
  2. The method splits strings at line boundaries.
  3. In file operations, refers to converting objects to byte streams.
  4. The function converts numbers to strings before file writing.