File Processing (Python) important MCQs in English/Hindi | Nielit Notes | PDF

In this post, we cover all important Questions from the topic "File Processing (Python)".

These MCQs will definitely help to pass IT and Computer students in any exam and interview that have File Processing topic.

The File Processing chapter covers the following topics:

Concept of Files, File opening in various modes and closing of a file, Reading

from a file, Writing onto a file, File functions-open(), close(), read(),

readline(),readlines(),write(), writelines(),tell(),seek(), Command Line arguments.

After completion of the File Processing unit in Python, Students will be able to Work with files and reading /writing onto files.


File Processing MCQs

Here are the 47 most important and frequently asked MCQs from the  "File Processing (Python)" topic.


1. To read the next line of the file from a file object infile, we use ____________

a. infile.read(2)

b. infile.read()

c. infile.readline()

d. infile.readlines()

Ans.c



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

a. Read()

b. Readcharacters()

c. Readall()

d. Readchar()

Ans.a



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

a. Readline()

b. Readlines()

c. Readstatement()

d. Readfullline()

Ans.b


 

4. To open a file c:\scores.txt for appending data, we use ____________

a. outfile = open(“c:\\scores.txt”, “a”)

b. outfile = open(“c:\\scores.txt”, “rw”)

c. outfile = open(file = “c:\scores.txt”, “w”)

d. outfile = open(file = “c:\\scores.txt”, “w”)

Ans.a



5. Which of the following statements are true?

a. When you open a file for reading, if the file does not exist, an error occurs

b. When you open a file for writing, if the file does not exist, a new file is created

c. When you open a file for writing, if the file exists, the existing file is overwritten with the new file

d. All of the mentioned.

Ans.d



6. Which of the following commands can be used to read “n” number of characters from a file using the file object <file>?

a. file.read(n)

b. n = file.read()

c. file.readline(n)

d. file.readlines()

Ans.a


 

7. Which of the following commands can be used to read the remaining lines in a file using the file object <tmpfile>?

a. tmpfile.read(n)

b. tmpfile.read()

c. tmpfile.readline()

d. tmpfile.readlines()

Ans.d

 


8. What does the <readlines()> method returns?

a. str

b. a list of lines

c. list of single characters

d. list of integers

Ans.b

 


9. To read the remaining lines of the file from a file object infile, we use ____________

a. infile.read(2)

b. infile.read()

c. infile.readline()

d. infile.readlines()

Ans.d



10. Which of the following statements correctly explain the function of seek() method?

a. tell the current position within the file.

b. indicate that the next read or write occurs from that position in a file.

c. determine if you can move the file position or not.

d. move the current file position to a different location at a defined offset.

Ans.d



11. Which of the following statements correctly explain the function of truncate() method?

a. truncates the file’s size

b. deletes the content of the file

c. truncates the file’s size and returns that content

d. None of the above

Ans.a



12. Which of the following functions can be used to check if a file “logo” exists?

a. os.path.isFile(logo)

b. os.path.exists(logo)

c. os.path.isfile(logo)

d. os.isFile(logo)

Ans.c

 


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

a. Close()

b. Stop()

c. End()

d. Closefile()

Ans.a



14. Which of the following commands can be used to read the entire contents of a file as a string using the file object <tmpfile>?

a. tmpfile.read(n)

b. tmpfile.read()

c. tmpfile.readline()

d. tmpfile.readlines()

Ans.b

 


15. Which of the following commands can be used to read the next line in a file using the file object <tmpfile>?

a. tmpfile.read(n)

b. tmpfile.read()

c. tmpfile.readline()

d. tmpfile.readlines()

Ans.c



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

a. Yes

b. No

c. Machine dependent

d. All of the mentioned

Ans.a



17. Which of the following functions displays a file dialog for opening an existing file?

a. tmpfile = askopenfilename()

b. tmpfile = asksaveasfilename()

c. tmpfile = openfilename()

d. tmpfile = saveasfilename()

Ans.a

 


18. Which of the following functions displays a file dialog for saving a file?

a. tmpfile = askopenfilename()

b. tmpfile = openfilename()

c. tmpfile = asksaveasfilename()

d. tmpfile = saveasfilename()

Ans.c

 


19. Which of the following functions do you use to write data in the binary format?

a. write

b. output

c. dump

d. send

Ans.c



20. What are the two built-in functions to read a line of text from standard input, which is by default the keyboard?

a. Raw_input

b. Input

c. Read

d. Scanner

Ans.a and b

 


21. What will be the output of the following code snippet?

txt = input("Enter your input: ")

print ("Received input is : ", txt)

a. Enter your input: Hello Viewers

    Received input is : Hello Viewers

b. Enter your input: [x*5 for x in range(2,10,2)]

    Received input is : [10, 20, 30, 40]

c. Both are correct

d. None of the above

Ans.a

 


22. Which of the following statements correctly explain the function of tell() method?

a. tells the current position within the file.

b. indicates that the next read or write will occur at that many bytes from the beginning of the file.

c. move the current file position to a different location.

d. it changes the file position only if allowed to do so else returns an error.

Ans. a and b

 


23. Which of the following statements correctly define pickling in python?

a. It is a process to convert a Python object into a byte stream.

b. It is a process to convert a byte stream to Python object.

c. It is done using two methods dump and load.

d. Serialization is an alternate name for pickling.

Ans.a, C, and D



24. What is the correct syntax of open() function?

a. file = open(file_name [, access_mode][, buffering])

b. file object = open(file_name [, access_mode][, buffering])

c. file object = open(file_name)

d. None of the above

Ans.b



25. Which of the following statements is correct regarding the seek() method?

a. sets the file position to the end of file

b. position of file pointer remains unchanged

c. error occurs

d. the file pointer is set to the start of the file

Ans.c



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

a. Read

b. Write

c. Append

d. None of the mentioned

Ans.c



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

a. write()

b. writecharacters()

c. writeall()

d. writechar()

Ans.a



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

a. writeline()

b. writelines()

c. writestatement()

d. writefullline()

Ans.a



29. 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+

Ans.a


30. EOL stands for:

a. End of list

b. End of line

c. End of Length

d. End of Level

Ans. a. End of list



31. To open a file c:\scores.txt for reading, we use _____________

a. infile = open(“c:\scores.txt”, “r”)

b. infile = open(“c:\\scores.txt”, “r”)

c. infile = open(file = “c:\scores.txt”, “r”)

d. infile = open(file = “c:\\scores.txt”, “r”)

Ans.b



32. What will be the output of the following code snippet?

txt = eval(input("Enter your input: "))

print ("Received input is : ", txt)

a. Enter your input: Hello Viewers

    Received input is : Hello Viewers

b. Enter your input: [x*5 for x in range(2,10,2)]

    Received input is : [10, 20, 30, 40]

c. Both are correct

d. None of the above

Ans.b

 


33. Which of the following are the attributes related to a file object?

a. closed

b. mode

c. name

d. rename

Ans.a, b and c



34. Which of the following statements are true?

a. When you open a file for reading, if the file does not exist, an error occurs

b. When you open a file for writing, if the file does not exist, a new file is created

c. When you open a file for writing, if the file exists, the existing file is overwritten with the new file

d. All of the mentioned

Ans.d



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

a. Read

b. Write

c. Append

d. None of the mentioned

Ans.b



36. To read two characters from a file object infile, we use ____________

a. infile.read(2)

b. infile.read()

c. infile.readline()

d. infile.readlines()

Ans.a



37. To read the entire remaining contents of the file as a string from a file object infile, we use ____________

a. infile.read(2)

b. infile.read()

c. infile.readline()

d. infile.readlines()

Ans.b



38. The readlines() method returns ____________

a. str

b. a list of lines

c. a list of single characters

d. a list of integers

Ans.b



39. To open a file c:\scores.txt for reading, we use _____________

a. infile = open(“c:\scores.txt”, “r”)

b. infile = open(“c:\\scores.txt”, “r”)

c. infile = open(file = “c:\scores.txt”, “r”)

d. infile = open(file = “c:\\scores.txt”, “r”)

Ans.b



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

a. read, append

b. append, read

c. write, append

d. none of the mentioned

Ans.a



41. To open a file c:\scores.txt for writing, we use ____________

a. outfile = open(“c:\scores.txt”, “w”)

b. outfile = open(“c:\\scores.txt”, “w”)

c. outfile = open(file = “c:\scores.txt”, “w”)

d. outfile = open(file = “c:\\scores.txt”, “w”)

Ans.b



42.  Which of the following command is used to open a file “c:\temp.txt” in read-mode only?

a. infile = open(“c:\temp.txt”, “r”)

b. infile = open(“c:\\temp.txt”, “r”)

c. infile = open(file = “c:\temp.txt”, “r+”)

d. infile = open(file = “c:\\temp.txt”, “r+”)

Ans.b

 


43. Which of the following command is used to open a file “c:\temp.txt” for writing in binary format only?

a. outfile = open(“c:\temp.txt”, “w”)

b. outfile = open(“c:\\temp.txt”, “wb”)

c. outfile = open(“c:\temp.txt”, “w+”)

d. outfile = open(“c:\\temp.txt”, “wb+”)

Ans.b

 


44. Which of the following command is used to open a file “c:\temp.txt” for reading in binary format only?

a. outfile = open(“c:\temp.txt”, “r”)

b. outfile = open(“c:\\temp.txt”, “rb”)

c. outfile = open(“c:\temp.txt”, “r+”)

d. outfile = open(“c:\\temp.txt”, “rb+”)

Ans.b



45. To open a file c:\scores.txt for appending data, we use ____________

a. outfile = open(“c:\\scores.txt”, “a”)

b. outfile = open(“c:\\scores.txt”, “rw”)

c. outfile = open(file = “c:\scores.txt”, “w”)

d. outfile = open(file = “c:\\scores.txt”, “w”)

Ans.a



46.  Which of the following command is used to open a file “c:\temp.txt” in write-mode only?

a. outfile = open(“c:\temp.txt”, “w”)

b. outfile = open(“c:\\temp.txt”, “w”)

c. outfile = open(file = “c:\temp.txt”, “w+”)

d. outfile = open(file = “c:\\temp.txt”, “w+”)

Ans.b



47. What will be the output of the following Python code?

f = None

for i in range (5):

    with open("data.txt", "w") as f:

        if i > 2:

            break

print(f.closed)

a) True

b) False

Ans.a

No comments:

Powered by Blogger.