Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
Python - File Handling Fundamentals
What will be the output of this code?
f = open('data.txt', 'r')
content = f.read()
f.close()
print(type(content))
A<class 'bytes'>
BNone
CError
D<class 'str'>
Step-by-Step Solution
Solution:
  1. Step 1: Understand read() return type

    Reading a file in text mode returns a string.
  2. Step 2: Check type of content

    content is a string, so type(content) is <class 'str'>.
  3. Final Answer:

    <class 'str'> -> Option D
  4. Quick Check:

    read() in text mode returns str = B [OK]
Quick Trick: read() returns string in text mode, bytes in binary mode [OK]
Common Mistakes:
  • Expecting bytes instead of string
  • Forgetting to close file
  • Assuming read() returns None

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes