PRACTICE • LEARN • GROW
Module 10: File Handling - Final Quiz MCQs
Solve topic-wise placement questions and improve your skills.
Module 10: File Handling - Final Quiz Progress
0 of 11 completed
0%
Topic Track
You are viewing MCQs in preview mode. Log in to select options, submit answers, and track your progress.
Log In to Answer
1
What is the primary benefit of opening files with the
with statement in Python?
A.
It makes disk access 10x faster in hardware
B.
It implements a context manager that guarantees the file is automatically closed upon block exit, even if exceptions are raised
C.
It automatically parses files into JSON
D.
It prevents other operating system processes from reading the file
Explanation & Key
Correct Answer:
2
What happens when you open an existing file using the
'w' (write) mode?
A.
New text is appended to the end of the existing content
B.
The existing contents of the file are immediately truncated (erased completely)
C.
Raises a FileExistsError
D.
The file is opened in read-only mode
Explanation & Key
Correct Answer:
3
Which technique is the most memory-efficient when processing a massive 20 GB log file in Python?
A.
lines = f.read().split('\n')
B.
lines = f.readlines()
C.
for line in f: (lazy line-by-line streaming iteration)
D.
data = [line for line in f.read()]
Explanation & Key
Correct Answer:
4
What does
f.readline() return when it reaches the End-Of-File (EOF)?
A.
None
B.
An empty string: ''
C.
Raises an EOFError
D.
-1
Explanation & Key
Correct Answer:
5
Does
f.write('Hello') automatically append a newline character to the end of the text?
A.
Yes, identical to print()
B.
No, write() writes exactly what is provided; newlines must be included explicitly ('Hello\n')
C.
Only on Windows operating systems
D.
Only when the file is opened in binary mode
Explanation & Key
Correct Answer:
6
Which file open mode should you select to open a file for both reading and writing simultaneously without truncating?
A.
'rw'
B.
'r+'
C.
'w+'
D.
'a+'
Explanation & Key
Correct Answer:
7
What exception is raised when trying to open a file that does not exist in
'r' mode?
A.
MissingFileException
B.
FileNotFoundError
C.
IOError: No file
D.
NullPointerException
Explanation & Key
Correct Answer:
8
What method returns the current byte position of the file pointer?
A.
f.cursor()
B.
f.tell()
C.
f.pos()
D.
f.seek()
Explanation & Key
Correct Answer:
9
Which two dunder methods must a custom class implement to support the
with context manager protocol?
A.
__open__ and __close__
B.
__enter__ and __exit__
C.
__start__ and __stop__
D.
__init__ and __del__
Explanation & Key
Correct Answer:
10
Why is explicitly specifying
encoding='utf-8' strongly recommended when calling open()?
A.
Because files cannot be read without an encoding specified
B.
Because the default encoding is platform-dependent (e.g. cp1252 on Windows vs utf-8 on Linux), causing cross-platform encoding crashes if omitted
C.
To compress the text in RAM
D.
To encrypt the file on disk
Explanation & Key
Correct Answer:
11
What data type is returned when reading from a file opened in binary mode (
'rb')?
A.
str
B.
bytes
C.
bytearray
D.
int
Explanation & Key
Correct Answer:
Question Palette
Answered
Current
Not Attempted
Wrong