Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Python - File Handling Fundamentals
Identify the error in this code snippet:
f = open('file.txt', 'r')
print(f.read())
print(f.read())
f.close()
AFile mode 'r' is incorrect
BSecond read() returns empty string because file pointer is at end
Cread() cannot be called twice
DFile is not closed properly
Step-by-Step Solution
Solution:
  1. Step 1: Understand file pointer behavior

    After first read(), file pointer moves to end of file.
  2. Step 2: Effect of second read()

    Second read() returns empty string because no more data to read.
  3. Final Answer:

    Second read() returns empty string because file pointer is at end -> Option B
  4. Quick Check:

    File pointer at end causes empty read = D [OK]
Quick Trick: File pointer moves; second read() after first returns empty [OK]
Common Mistakes:
  • Thinking read() resets file pointer
  • Assuming error on second read()
  • Confusing file mode

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes