Python MCQ’s on Files: Set 5

41. Which of the following is not a valid mode to open a file?

a) ab

b) rw

c) r+

d) w+

Check Answer

Answer: b
Use r+, w+ or a+ to perform both read and write operations using a single file object.

42. What is the difference between r+ and w+ modes?

a) no difference

b) in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+

c) in w+ the pointer is initially placed at the beginning of the file and the pointer is at the end for r+

d) depends on the operating system

Check Answer

Answer: b
none.

43. How do you get the name of a file from a file object (fp)?

a) fp.name

b) fp.file(name)

c) self.__name__(fp)

d) fp.__name__()

Check Answer

Answer: a
name is an attribute of the file object.

44. Which of the following is not a valid attribute of a file object (fp)?

a) fp.name

b) fp.closed

c) fp.mode

d) fp.size

Check Answer

Answer: d
fp.size has not been implemented.

45. How do you close a file object (fp)?

a) close(fp)

b) fclose(fp)

c) fp.close()

d) fp.__close__()

Check Answer

Answer: c
close() is a method of the file object.

46. How do you get the current position within the file?

a) fp.seek()

b) fp.tell()

c) fp.loc

d) fp.pos

Check Answer

Answer: b
It gives the current position as an offset from the start of file.

47. How do you rename a file?

a) fp.name = ‘new_name.txt’

b) os.rename(existing_name, new_name)

c) os.rename(fp, new_name)

d) os.set_name(existing_name, new_name)

Check Answer

Answer: b
os.rename() is used to rename files.

48. How do you delete a file?

a) del(fp)

b) fp.delete()

c) os.remove(‘file’)

d) os.delete(‘file’)

Check Answer

Answer: c
os.remove() is used to delete files.

49. How do you change the file position to an offset value from the start?

a) fp.seek(offset, 0)

b) fp.seek(offset, 1)

c) fp.seek(offset, 2)

d) none of the mentioned

Check Answer

Answer: a
0 indicates that the offset is with respect to the start.

50. What happens if no arguments are passed to the seek function?

a) file position is set to the start of file

b) file position is set to the end of file

c) file position remains unchanged

d) error

Check Answer

Answer: d
seek() takes at least one argument.

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