Python Certification Questions & Answers 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: None.
4. Which one of the following is not attributes of file?
a) closed
b) softspace
c) rename
d) mode
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: 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
