Python MCQ’s on Files: Set 2

 

11. 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

Check Answer

Answer: a
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

12. 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

Check Answer

Answer: a
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!”

13. 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

Check Answer

Answer: a
None.

14. Which one of the following is not attributes of file?

a) closed

b) softspace

c) rename

d) mode

Check Answer

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

15. 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

Check Answer

Answer: a
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.

16. 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

Check Answer

Answer: a
This is the correct syntax which has shown below.
rename(current_file_name, new_file_name)

17. 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

Check Answer

Answer: a
remove(file_name)

18. 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

Check Answer

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

19. 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

Check Answer

Answer: a
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.

20. 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

Check Answer

Answer: a
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.

Page  1  2  3  4  5

Python Interview Questions (MCQs)

We have divided these Python Questions and Answers into various parts based on the topics. Open the Topic of your choice and Practice these MCQs.

Python MCQs on Variables and Operators Python MCQs on Precedence and Associativity
Python MCQs on Data Type Python MCQs on Boolean
Python MCQs on Bitwise Operators Python MCQs on Formatting and Advance Formatting
Python MCQs on Decorators Python MCQs on While and For Loops
Python MCQs on List Python MCQs on List Comprehension
Python MCQs on String Python MCQs on Tuple
Python MCQs on SET Python MCQs on Dictionary
Python MCQs on Functions Python MCQs on Argument Parsing
Python MCQs on Global and Local Variables Python MCQs on Recursion
Python MCQs on Mapping Functions Python MCQs on Modules
Python MCQs on Regular Expressions Python MCQs on Files
Python MCQs on Overloading Python MCQs on Classes and Objects
Python MCQs on Inheritance Python MCQs on Polymorphism and Encapsulation
Python MCQs on Exception Handling

Leave a Comment