Python File Handling Quiz for Programmers

Welcome to the Python file handling quiz part-2 intended for experienced Python programming professionals.

This time, we’ve raised the complexity of questions from our last quiz where we focused on the file handling basics of Python.

In the last test, we mainly concentrated on the topics like Python file routines and the access modes. Whereas, in this quiz, you’ll find coding snippets working with a combination of functions and other Python constructs. And then, you’ll have to evaluate their output to determine the correct answer.

However, if you want to go through a quick recap of the important Python file handling functionality, then go over the following tutorial. It contains the gist of file I/O operations, functions, access modes, and some valuable examples.

It’s always exciting to evaluate the skills that we learned. So attempt the Python file handling quiz part-2, and see how you score.

Python file handling quiz part-2 for Programmers.

Python file handling quiz part-2 for Experienced Programmers

Q-1. 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

Click here to view the answer.
Answer. A and B

 

Q-2. What will be the output of the following code snippet?

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

Click here to view the answer.
Answer. A

 

Q-3. What will be the output of the following code snippet?

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

Click here to view the answer.
Answer. B

 

Q-4. Which of the following are the attributes related to a file object?

A. closed
B. mode
C. name
D. rename

Click here to view the answer.
Answer. A, B and C

 

Q-5. 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.

Click here to view the answer.
Answer. A, and B

 

Q-6. 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.

Click here to view the answer.
Answer. D

 

Q-7. What will be the output of the following code snippet?

A.  TechBeamers
     Hello viewers!!
B. Name of the file: myfile.txt
     TechBeamers
     Hello Viewers!!
C. TechBeamers  Hello viewers!!
D. Syntax Error

Click here to view the answer.
Answer. B

 

Q-8. 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

Click here to view the answer.
Answer. A

 

Q-9. What will be the output of the following code snippet?

Assuming that input given by user is :Tech Beamers
A. Tech Beamers
B. Tech
C. Tech
     Beamers
D. Error

Click here to view the answer.
Answer. A

 

Q-10. 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.

Click here to view the answer.
Answer. A, C, and D

 

Q-11. 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

Click here to view the answer.
Answer. B

 

Q-12. What will be the output of the following code snippet?

A. Name of the file:  myfile.txt
Read Line: [‘This is 1st line, This is 2nd line, This is 3rd line’]

B. Name of the file:  myfile.txt
Read Line: [‘ This is 2nd line, This is 3rd line’]

C. Read Line: [ ‘This is 1st line’]
D. Runtime Error

Click here to view the answer.
Answer. A

 

Q-13. What will be the output of the following code snippet?

A. Runtime Error
B.  Hello World how are you today
C. [‘Hello’, ‘World’, ‘how’, ‘are’, ‘you’, ‘today’]
D. Hello

Click here to view the answer.
Answer. C

 

Q-14. What will be the output of the following code snippet?

A. Runtime Error
B. True
C. False
D. Hello world

Click here to view the answer.
Answer. B

 

Q-15. What will be the output of the following code snippet?

A. Runtime Error
B. [‘This is 1st line\n’, ‘ This is 2nd line\n’, ‘This is 3rd line’]
C. [‘ This is 2nd line\n’, ‘This is 3rd line’]
D. []

Click here to view the answer.
Answer. D

 

Q-16. What will be the output of the following code snippet?

A. [‘ This is 1st line\n’]
B. []
C. IO Error
D. None

Click here to view the answer.
Answer. C

 

Q-17. What will be the output of the following code snippet?

A. This is the test file for exception handling!!
B. Error: could not find a file or read data
C. content is written in the file successfully
D. IO Error

Click here to view the answer.
Answer. C

 

Q-18. 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

Click here to view the answer.
Answer. C

 

Q-19. What will be the output of the following code snippet?

A. red
     yellow
     blue
B. [‘red\n’, ‘yellow\n’, ‘blue\n’]
C. Error: I/O operation on closed file.
D. Compilation error

Click here to view the answer.
Answer. C

 

Q-20. Which of the following statements are correct regarding the file access modes?

A. ‘r+’ opens a file for both reading and writing. File object points to its beginning.
B. ‘w+’ opens a file for both writing and reading. Overwrites the existing file if it exists and creates a new one if it does not exist.
C. ‘wb’ opens a file for reading and writing in binary format. Overwrites the file if it exists and creates a new one if it does not exist.
D. ‘a’ opens a file for appending. The file pointer is at the end of the file if the file exists.

Click here to view the answer.
Answer. A, B, and D

 

Summary – Python file handling quiz part-2 for Programmers.

We hope that you’d enjoyed going through the above Python file handling quiz part-2. We host a no. of other quizzes on various programming topics in Python. You can follow the below section to find out the one that interests you the most.

Soon, we’ll also come up with a quiz on Python functions. Till then, continue reading and keep learning.

Thanks,

TechBeamers.

Leave a Reply


*