Test Series - python

Test Number 100/108

Q: In file handling, what does this terms means “r, a”?
A. read, append
B. append, read
C. write, append
D. none of the mentioned
Solution: r- reading, a-appending.
Q: What is the use of “w” in file handling?
A. Read
B. Write
C. Append
D. None of the mentioned
Solution: 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”).
Q: What is the use of “a” in file handling?
A. Read
B. Write
C. Append
D. None of the mentioned
Solution: 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”).
Q: Which function is used to read all the characters?
A. Read()
B. Readcharacters()
C. Readall()
D. Readchar()
Solution: The read function reads all characters fh = open(“filename”, “r”)
content = fh.read().
Q: Which function is used to read single line from file?
A. Readline()
B. Readlines()
C. Readstatement()
D. Readfullline()
Solution: The readline function reads a single line from the file fh = open(“filename”, “r”)
content = fh.readline().
Q: Which function is used to write all the characters?
A. write()
B. writecharacters()
C. writeall()
D. writechar()
Solution: To write a fixed sequence of characters to a file
fh = open(“hello.txt”,”w”)
write(“Hello World”).
Q: Which function is used to write a list of string in a file?
A. writeline()
B. writelines()
C. writestatement()
D. writefullline()
Solution: 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).
Q: Which function is used to close a file in python?
A. Close()
B. Stop()
C. End()
D. Closefile()
Solution: f.close()to close it and free up any system resources taken up by the open file.
Q: Is it possible to create a text file in python?
A. Yes
B. No
C. Machine dependent
D. All of the mentioned
Solution: 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
”)
file.write(“and another line
”)
file.close().
Q: 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+
Solution: 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.

You Have Score    /10