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
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
c) Your name is: san
d) Enter your name: sanfoundry Your name is: sanfoundry
View Answer
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:
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 PythonView Answer
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
4. Which of the following mode will refer to binary data?
a) r
b) w
c) +
d) b
View Answer
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
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
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
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
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
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
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.
- Check Python Books
- Apply for Python Internship
- Check Information Technology Books
- Practice Programming MCQs
- Apply for Programming Internship