This set of Python Multiple Choice Questions & Answers (MCQs) focuses on “Files”.
1. Which are the two built-in functions to read a line of text from standard input, which by default comes from the keyboard?
a) Raw_input & Input
b) Input & Scan
c) Scan & Scanner
d) Scanner
View Answer
Explanation: Python provides two built-in functions to read a line of text from standard input, which by default comes from the keyboard. These functions are:
raw_input and input
2. What will be the output of the following Python code?
str = raw_input("Enter your input: ");
print "Received input is : ", str
a)
Enter your input: Hello Python Received input is : Hello Python
b)
Enter your input: Hello Python Received input is : Hello
c)
Enter your input: Hello Python Received input is : Python
d) None of the mentioned
View Answer
Explanation: The raw_input([prompt]) function reads one line from standard input and returns it as a string. This would prompt you to enter any string and it would display same string on the screen. When I typed “Hello Python!”
3. What will be the output of the following Python code?
str = input("Enter your input: ");
print "Received input is : ", str
a)
Enter your input: [x*5 for x in range(2,10,2)] Received input is : [x*5 for x in range(2,10,2)]
b)
Enter your input: [x*5 for x in range(2,10,2)] Received input is : [10, 30, 20, 40]
c)
Enter your input: [x*5 for x in range(2,10,2)] Received input is : [10, 10, 30, 40]
d) None of the mentioned
View Answer
Explanation: None.
4. Which one of the following is not attributes of file?
a) closed
b) softspace
c) rename
d) mode
View Answer
Explanation: rename is not the attribute of file rest all are files attributes.
Attribute Description file.closed Returns true if file is closed, false otherwise. file.mode Returns access mode with which file was opened. file.name Returns name of the file. file.softspace Returns false if space explicitly required with print, true otherwise.
5. What is the use of tell() method in python?
a) tells you the current position within the file
b) tells you the end position within the file
c) tells you the file is opened or not
d) none of the mentioned
View Answer
Explanation: The tell() method tells you the current position within the file; in other words, the next read or write will occur at that many bytes from the beginning of the file.
6. What is the current syntax of rename() a file?
a) rename(current_file_name, new_file_name)
b) rename(new_file_name, current_file_name,)
c) rename(()(current_file_name, new_file_name))
d) none of the mentioned
View Answer
Explanation: This is the correct syntax which has shown below.
rename(current_file_name, new_file_name)
7. What is the current syntax of remove() a file?
a) remove(file_name)
b) remove(new_file_name, current_file_name,)
c) remove(() , file_name))
d) none of the mentioned
View Answer
Explanation: remove(file_name)
8. What will be the output of the following Python code?
fo = open("foo.txt", "rw+")
print "Name of the file: ", fo.name
# Assuming file has following 5 lines
# This is 1st line
# This is 2nd line
# This is 3rd line
# This is 4th line
# This is 5th line
for index in range(5):
line = fo.next()
print "Line No %d - %s" % (index, line)
# Close opened file
fo.close()
a) Compilation Error
b) Syntax Error
c) Displays Output
d) None of the mentioned
View Answer
Explanation: It displays the output as shown below. The method next() is used when a file is used as an iterator, typically in a loop, the next() method is called repeatedly. This method returns the next input line, or raises StopIteration when EOF is hit.
Output:
Name of the file: foo.txt Line No 0 - This is 1st line Line No 1 - This is 2nd line Line No 2 - This is 3rd line Line No 3 - This is 4th line Line No 4 - This is 5th line
9. What is the use of seek() method in files?
a) sets the file’s current position at the offset
b) sets the file’s previous position at the offset
c) sets the file’s current position within the file
d) none of the mentioned
View Answer
Explanation: Sets the file’s current position at the offset. The method seek() sets the file’s current position at the offset.
Following is the syntax for seek() method:
fileObject.seek(offset[, whence])
Parameters
offset — This is the position of the read/write pointer within the file.
whence — This is optional and defaults to 0 which means absolute file positioning, other values are 1 which means seek relative to the current position and 2 means seek relative to the file’s end.
10. What is the use of truncate() method in file?
a) truncates the file size
b) deletes the content of the file
c) deletes the file size
d) none of the mentioned
View Answer
Explanation: The method truncate() truncates the file size. Following is the syntax for truncate() method:
fileObject.truncate( [ size ])
Parameters
size — If this optional argument is present, the file is truncated to (at most) that size.
Sanfoundry Global Education & Learning Series – Python.
To practice all areas of Python, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Apply for Python Internship
- Check Python Books
- Apply for Programming Internship
- Practice Programming MCQs
- Check Information Technology Books