250+ TOP MCQs on Python Files and Answers

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?

  1. str = raw_input("Enter your input: ");
  2. 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?

  1. str = input("Enter your input: ");
  2. 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

Answer: c
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?

  1. fo = open("foo.txt", "rw+")
  2. print "Name of the file: ", fo.name
  3.  
  4. # Assuming file has following 5 lines
  5. # This is 1st line
  6. # This is 2nd line
  7. # This is 3rd line
  8. # This is 4th line
  9. # This is 5th line
  10.  
  11. for index in range(5):
  12.    line = fo.next()
  13.    print "Line No %d - %s" % (index, line)
  14.  
  15. # Close opened file
  16. fo.close()

a) Compilation Error
b) Syntax Error
c) Displays Output
d) None of the mentioned

Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: a
Clarification: The method truncate() truncates the file size. Following is the syntax for truncate() method:
fileObject.truncate( [ size ])

  250+ TOP MCQs on Python Turtle Module and Answers

Parameters
size — If this optional argument is present, the file is truncated to (at most) that size.

certification questions on Python,

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top