Python Questions and Answers – Files – 3

This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Files”.

1. Which is/are the basic I/O connections in file?
a) Standard Input
b) Standard Output
c) Standard Errors
d) All of the mentioned
View Answer

Answer: d
Explanation: The basic I/O connections in files include Standard Input (stdin), Standard Output (stdout), and Standard Errors (stderr), which are fundamental streams used for input and output operations.

  • Standard Input is used to receive input data (usually from the keyboard).
  • Standard Output is used to display output data (usually to the screen).
  • Standard Errors is used to output error messages separately from standard output, allowing error handling and logging.

2. What will be the output of the following Python code? (If entered name is sanfoundry)

import sys
print('Enter your name: ', end='')
name = ''
while True:
    c = sys.stdin.read(1)
    if c == '\n':
        break
    name += c
 
print('Your name is:', name)

a) sanfoundry
b)

Enter your name: sanfoundry
   Your name is: sanfoundry
advertisement

c) Your name is: san
d) Enter your name: sanfoundry Your name is: sanfoundry
View Answer

Answer: b
Explanation: This code reads one character at a time from standard input using sys.stdin.read(1).
It continues reading until a newline character (\n) is encountered (when the user presses Enter).
If the input is sanfoundry, then name becomes sanfoundry. The final print statement displays:

Free 30-Day Python Certification Bootcamp is Live. Join Now!
Your name is: sanfoundry

3. What will be the output of the following Python code?

import sys
sys.stdout.write(' Hello\n')
sys.stdout.write('Python\n')

a) Compilation Error
b) Runtime Error
c) Hello Python
d)

 Hello
Python
View Answer
Answer: d
Explanation: The sys.stdout.write() function writes text to the output without adding a newline automatically. Since both strings in the code include \n, each line is printed on a new line as written. Therefore, the output is:

Hello
Python
 
 

advertisement

4. Which of the following mode will refer to binary data?
a) r
b) w
c) +
d) b
View Answer

Answer:d
Explanation: The ‘b’ mode in file operations refers to binary mode. It’s used when reading or writing binary files (like images, videos, or executable files). For example:

  • ‘rb’ opens a file for reading in binary mode.
  • ‘wb’ opens a file for writing in binary mode.

Other modes like ‘r’, ‘w’, and ‘+’ relate to text file operations or read/write permissions, not specifically binary data.

5. What is the pickling?
a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned
View Answer

Answer: a
Explanation: Pickling is the process of converting a Python object into a byte stream, also known as serialization. This enables the object to be saved to a file or transmitted over a network. The reverse process, converting the byte stream back into a Python object, is called unpickling or deserialization. The pickle.dump() function is used to serialize (pickle) an object, while pickle.load() is used to deserialize (unpickle) it.

6. What is unpickling?
a) It is used for object serialization
b) It is used for object deserialization
c) None of the mentioned
d) All of the mentioned
View Answer

Answer: b
Explanation: Unpickling is the process of deserializing a pickled (serialized) object — that is, converting the byte stream back into a Python object. It is the reverse of pickling and is done using functions like pickle.load().

7. What is the correct syntax of open() function?
a) file = open(file_name [, access_mode][, buffering])
b) file object = open(file_name [, access_mode][, buffering])
c) file object = open(file_name)
d) none of the mentioned
View Answer

Answer: a
Explanation: The correct and general syntax of the open() function in Python is:

file = open(file_name [, access_mode][, buffering])
  • file_name: Name of the file to be opened.
  • access_mode: Optional; specifies the mode in which the file is opened (‘r’, ‘w’, ‘a’, ‘rb’, etc.).
  • buffering: Optional; sets buffering policy.

8. What will be the output of the following Python code?

fo = open("foo.txt", "wb")
print("Name of the file: ", fo.name)
fo.flush()
fo.close()

a) Compilation Error
b) Runtime Error
c) No Output
d) Flushes the file when closing them
View Answer

Answer: d
Explanation: The code opens a file in binary write mode and prints its name. The flush() method ensures any buffered data is written to the file. When close() is called, it automatically flushes the buffer as well, so the file is safely saved.

9. Correct syntax of file.writelines() is?
a) file.writelines(sequence)
b) fileObject.writelines()
c) fileObject.writelines(sequence)
d) none of the mentioned
View Answer

Answer: c
Explanation: The writelines() method is called on a file object and takes a sequence (like a list or tuple) of strings as its argument. It writes each string in the sequence to the file without adding newlines automatically. So, the proper syntax is:

fileObject.writelines(sequence)

10. Correct syntax of file.readlines() is?
a) fileObject.readlines( sizehint );
b) fileObject.readlines();
c) fileObject.readlines(sequence)
d) none of the mentioned
View Answer

Answer: a
Explanation: The readlines() method reads all the lines from a file and returns them as a list of strings. It optionally accepts a sizehint argument, which is an approximate number of bytes to read. The correct syntax is:

fileObject.readlines(sizehint)

where sizehint is optional.

Sanfoundry Global Education & Learning Series – Python.

To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 40s–60s and exploring new directions in your career, I also offer mentoring. Learn more here.