Python MCQ’s on Files: Set 4

31. In file handling, what does this terms means “r, a”?

a) read, append

b) append, read

c) write, append

d) none of the mentioned

Check Answer

Answer: a
r- reading, a-appending.

32. What is the use of “w” in file handling?

a) Read

b) Write

c) Append

d) None of the mentioned

Check Answer

Answer: b
This opens the file for writing. It will create the file if it doesn’t exist, and if it does, it will overwrite it.
fh = open(“filename_here”, “w”).

33. What is the use of “a” in file handling?

a) Read

b) Write

c) Append

d) None of the mentioned

Check Answer

Answer: c
This opens the fhe file in appending mode. That means, it will be open for writing and everything will be written to the end of the file.
fh =open(“filename_here”, “a”).

34. Which function is used to read all the characters?

a) Read()

b) Readcharacters()

c) Readall()

d) Readchar()

Check Answer

Answer: a
The read function reads all characters fh = open(“filename”, “r”)
content = fh.read().

35. Which function is used to read single line from file?

a) Readline()

b) Readlines()

c) Readstatement()

d) Readfullline()

Check Answer

Answer: b
The readline function reads a single line from the file fh = open(“filename”, “r”)
content = fh.readline().

36. Which function is used to write all the characters?

a) write()

b) writecharacters()

c) writeall()

d) writechar()

Check Answer

Answer: a
To write a fixed sequence of characters to a file
fh = open(“hello.txt”,”w”)
write(“Hello World”).

37. Which function is used to write a list of string in a file?

a) writeline()

b) writelines()

c) writestatement()

d) writefullline()

Check Answer

Answer: a
With the writeline function you can write a list of strings to a file
fh = open(“hello.txt”, “w”)
lines_of_text = [“a line of text”, “another line of text”, “a third line”] fh.writelines(lines_of_text).

38. Which function is used to close a file in python?

a) Close()

b) Stop()

c) End()

d) Closefile()

Check Answer

Answer: a
f.close()to close it and free up any system resources taken up by the open file.

39. Is it possible to create a text file in python?

a) Yes

b) No

c) Machine dependent

d) All of the mentioned

Check Answer

Answer: a
Yes we can create a file in python. Creation of file is as shown below.
file = open(“newfile.txt”, “w”)
file.write(“hello world in the new file\n”)
file.write(“and another line\n”)
file.close().

40. Which of the following are the modes of both writing and reading in binary format in file?

a) wb+

b) w

c) wb

d) w+

Check Answer

Answer: a
Here is the description below
“w” Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
“wb” Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
“w+” Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
“wb+” Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

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