Class 12 Computer Science File Handling in Python MCQs Set 5

Python File Handling



File Handling in Python MCQ’s Set – 5

Multiple Choice Questions


81. Find the output of the given code .

myfile = open("sample.txt", "w")
t = myfile.write("anjeev")
print(t)
myfile.close()

a) anjeev

b) 6

c) six

d) None of these

Answer : b) 6

82. Find the output of the given code .

myfile = open("sample.txt", "w")
t = myfile.write(23456)
print(t)
myfile.close()

a) 23456

b) 5

c) Error

d) None of these

Answer : c) Error

83. Find the output of the given code .

myfile = open("sample.txt", "w")
t = myfile.write(str(23456))
print(t)
myfile.close()

a) 23456

b) 5

c) Error

d) None of these

Answer : b) 5

84. write( ) function actually writes data onto a ________.

a) memory

b) file

c) buffer

d) None of these

Answer : c) buffer

85. ________ method is use to clear the buffer and write contents in buffer to the file.

a) flush()

b) clear()

c) close()

d) None of these

Answer : a) flush( )

86. When the ______ method is executed, the contents from this buffer are moved to the file located on the permanent storage.

a) flush()

b) clear()

c) close()

d) None of these

Answer : c) close( )

87. The ______ method allows the programmers can forcefully write to the file as and when required.

a) flush()

b) clear()

c) close()

d) None of these

Answer : a) flush()

88. Which function can be use to remove leading and trailing whitespaces like spaces, tabs, newline characters.

a) strip()

b) rstrip()

c) lstrip()

d) All of these

Answer : d) All of these

89. Which method is use to write multiple strings to a file.

a) write()

b) writelines()

c) writeline()

d) None of these

Answer : b) writelines()

90. Does a writelines() method return the number of characters written in the file?

a) Yes

b) No

Answer : b) No

91. Find the output of the given code?

myfile = open("sample.txt", "w")
t = myfile.writelines(("mycstutorial", "site"))
print(t)
myfile.close()

a) 16

b) Null

c) None

d) None of these

Answer : c) None

92. What is the content of file “sample.txt”, after execution of the given code?

myfile = open("sample.txt", "w")
myfile.writelines(["mycstutorial", "is", "the", "best", "site"])
myfile.flush()
myfile.close()

a) mycstutorialisthebestsite

b) mycstutorial is the best site

c) mycstutorial

is

the

best

site

d) None of these

Answer : a) mycstutorialisthebestsite

93. What is the content of file “sample.txt”, after execution of the given code?

myfile = open("sample.txt", "w")
myfile.writelines(["mycstutorial\n", "is\n", "the\n", "best\n", "site"])
myfile.flush()
myfile.close()

a) mycstutorialisthebestsite

b) mycstutorial is the best site

c) mycstutorial

is

the

best

site

d) None of these

Answer : c) mycstutorial

is

the

best

site

94. Which function in Python, returns the current position of the file object in the file?

a) seek( )

b) tell()

c) get()

d) None of these

Answer : b) tell()

95. tell() function returns _____ type of value.

a) integer

b) float

c) string

d) None of these

Answer : a) integer

96. Which function in Python, is used to position the file object at a particular position in a file?

a) seek( )

b) tell()

c) get()

d) None of these

Answer : a) seek()

97. tell( ) function returns the current position from the _______ of the file.

a) end

b) middle

c) beginning

d) None of these

Answer : c) beginning

98. Find the output of the given code?

myfile = open("story.txt", "r")
myfile.read(4)
print(myfile.tell())
myfile.close()

a) 4

b) 5

c) None

d) None of these

Answer : a) 4

99. Identify the valid function call statement of seek() method.

a) seek(0)

b) file_object.seek(24, 0)

c) file_object.seek()

d) None of these

Answer : b) file_object.seek(24,0)

100. Find the output of the given code?

myfile = file("story.txt", "r")
myfile.read(4)
myfile.seek(10,1)
print(myfile.tell())
myfile.close()

a) 4

b) 10

c)14

d) None of these

Answer : c) 14


More MCQ’s are on the way, Refresh your page…

Python File Handling (MCQ’s)


Python Revision Tour – Multiple Choice Questions (MCQ’s)


Python Functions – Working with Functions (MCQ’s)


Thanks for visiting. Online Classes are available.

You cannot copy content of this page

Scroll to Top