Welcome to part 2 of the file handling quiz in Python intended for experienced Python programmers. This time, we’ve raised the complexity of questions from our first quiz on Python file handling where we focused on the basics.
In the last test, we mainly concentrated on the topics like Python file routines and 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.
It’s always exciting to evaluate the skills that we learned. So attempt this Python file handling quiz, and see how you score.
File Handling Quiz in Python for Experienced – Part II
Q-1. What are the two built-in functions in Python that read a line of text from Standard I/O (Keyboard)?
- Raw_input
- Input
- Read
- Scanner
Q-2. What will be the output of the following code snippet?
txt = input("Enter your input: ") print ("Received input is : ", txt) # Note: Enter input as mentioned in the options.
- Enter your input: Hello Viewers
Received input is: Hello Viewers - Enter your input:
[x*5 for x in range(2,10,2)]
Received input is : [10, 20, 30, 40] - Both are correct
- None of the above
Q-3. What will be the output of the following code snippet?
txt = eval(input("Enter your input: ")) print ("Received input is : ", txt) # Note: Enter input as mentioned in the options.
- Enter your input: Hello Viewers
Received input is: Hello Viewers - Enter your input:
[x*5 for x in range(2,10,2)]
Received input is : [10, 20, 30, 40] - Both are correct
- None of the above
Q-4. Which of the following are the attributes related to a file object?
- closed
- mode
- name
- rename
Q-5. Which of the following statements correctly explains the function of the tell() method?
- It tells the current position within the file.
- It indicates that the next read or write will occur at that many bytes from the beginning of the file.
- It moves the current file position to a different location.
- It changes the file position only if allowed to do so else returns an error.
Q-6. Which of the following methods in Python can be used to check if a file exists?
- os.exists(file_path)
- file.exists(file_path)
- os.path.exists(file_path)
- path.exists(file_path)
Q-7. What will be the output of the following code snippet?
fo = open("myfile.txt", "w+") print ("Name of the file: ", fo.name) # Assuming that the file contains these lines # TechBeamers # Hello Viewers!! seq="TechBeamers\nHello Viewers!!" fo.writelines(seq ) fo.seek(0,0) for line in fo: print (line) fo.close()
- TechBeamers
Hello viewers!! - Name of the file: myfile.txt
TechBeamers
Hello Viewers!! - TechBeamers Hello viewers!!
- Syntax Error
Q-8. Which statement correctly defines the function of Python’s truncate() call?
- It truncates the file to a size of 0.
- It deletes the content of the file.
- It truncates the file’s size and returns that content.
- None of the above
Q-9. What will be the output of the following code snippet?
import sys print ('Enter your name: ',) name = '' while True: c = sys.stdin.read(1) if c == '\n': break name = name + c print ('Entered name is:', name)
Assuming that input given by the user is: Tech Beamers
- Tech Beamers
- Tech
- Tech
Beamers - Error
Q-10. Which of the following statements correctly defines pickling in Python?
- It is a process to convert a Python object into a byte stream.
- It is a process to convert a byte stream to a Python object.
- It is done using two methods dump and load.
- Serialization is an alternate name for pickling.
Q-11. Which is the correct syntax of the open() function in Python?
- file = open(file_name [, access_mode][, buffering])
- file object = open(file_name [, access_mode][, buffering])
- file object = open(file_name)
- None of the above
Q-12. What will be the output of the following code snippet?
# Open a file in read-write mode fo = open("myfile.txt", "w+") print ("Name of the file: ", fo.name) # Assuming file has the following line txt = "This is 1st line," fo.writelines( txt ) seq = " This is 2nd line, This is 3rd line" # Write sequence of lines at the end of the file. fo.seek(0, 2) fo.writelines( seq ) # Now read complete file from beginning. fo.seek(0,0) line = fo.readlines() print ("The Read Line: %s" % (line)) # Close the file fo.close()
- Name of the file: myfile.txt
The Read Line: [‘This is 1st line, This is 2nd line, This is 3rd line’] - Name of the file: myfile.txt
The Read Line: [‘ This is 2nd line, This is 3rd line’] - The Read Line: [ ‘This is 1st line’]
- Runtime Error
Q-13. What will be the output of the following code snippet?
with open("hello.txt", "w") as f: f.write("Hello World how are you today") with open('hello.txt', 'r') as f: data = f.readlines() for line in data: words = line.split() print (words) f.close()
- Runtime Error
- Hello World how are you today
- [‘Hello’, ‘World’, ‘how’, ‘are’, ‘you’, ‘today’]
- Hello
Q-14. What will be the output of the following code snippet?
f = None for i in range (5): with open("myfile.txt", "w") as f: if i > 2: break print (f.closed)
- Runtime Error
- True
- False
- Hello world
Q-15. What will be the output of the following code snippet?
f = open("data.txt", "w+") txt = "This is 1st line\n" f.writelines( txt ) seq = " This is 2nd line\nThis is 3rd line" f.seek(0, 2) f.writelines( seq ) myList = [] for line in f: myList.append(line) print(myList) f.close()
- Runtime Error
- [‘This is 1st line\n’, ‘ This is 2nd line\n’, ‘This is 3rd line’]
- [‘ This is 2nd line\n’, ‘This is 3rd line’]
- []
Q-16. What will be the output of the following code snippet?
f = open("data.txt", "r") txt = "This is 1st line\n" f.writelines( txt ) f.seek(0,0) line = f.readlines() print ("Read Line: %s" % (line)) f.close()
- [‘ This is 1st line\n’]
- []
- IO Error
- None
Q-17. What will be the output of the following code snippet?
try: f = open("testfile", "r") f.write("This is the test file for exception handling!!") except IOError: print("Error: could not find a file or read data") else: print("The content is written in the file successfully")
- This is the test file for exception handling!!
- Error: could not find a file or read data
- The content is written in the file successfully
- IO Error
Q-18. Which statement is true for the Python seek() method?
- It sets the file position to the end of the file.
- The position of the file pointer remains unchanged.
- An error occurs.
- The file pointer is set to the start of the file.
Q-19. What will be the output of the following code snippet?
colors = ['red\n', 'yellow\n', 'blue\n'] f = open('colors.txt', 'w') f.writelines(colors) f.close() f.seek(0,0) for line in f: print (line)
- red
yellow
blue - [‘red\n’, ‘yellow\n’, ‘blue\n’]
- ValueError: I/O operation on closed file.
- Compilation error
Q-20. Which statements are correct regarding the file access modes in Python?
- The ‘r+’ mode opens a file for both reading and writing. The file object points to its beginning.
- The ‘w+’ mode 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.
- The ‘wb’ mode opens a file for writing in binary format. Overwrites the file if it exists and creates a new one if it does not exist.
- The ‘a’ mode opens a file for appending. The file pointer is at the end of the file if the file exists.
Q-21. What does the with statement in Python file handling ensure?
- Automatic file closing after file operations are completed.
- File access permissions are checked before opening the file.
- File compression is applied during read and write operations.
- The file is opened in binary mode for efficient data handling.
Q-22. When working with file objects in Python, what does the flush() method do?
A) The flush() method clears the contents of the file.
B) The flush() method moves the file pointer to the beginning of the file.
C) The flush() method writes any buffered data to the file.
D) The flush() method closes the file and releases system resources.
Q-23. Which of the following methods can be used to read a binary file in Python?
A) readline()
B) read()
C) readlines()
D) Binary files cannot be read in Python.
Q-24. What is the purpose of the os.rename() function in Python?
A) It renames a directory.
B) It renames a file.
C) It creates a new file.
D) It deletes a file.
Q-25. How can you retrieve the file name from a given file path in Python?
A) Using the get_file_name() method.
B) Using the file_name() method.
C) Using the basename() function from the os.path module.
D) It is not possible to retrieve the file name from a file path.
Summary – Evaluate File Handling Quiz in Python Part II
We hope that you enjoyed attempting part 2 of the file-handling quiz in Python.
But don’t just leave. We host a number 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.
Related Posts
- Python file handling quiz part I
- Software test automation quiz for beginners
- Linux basic questions and answers
- 30 questions on Python list, tuple, and dictionary
- Basic Python function quiz
Soon, we’ll also come up with more quizzes and Python tutorials. Till then, continue reading and keep learning.
Thanks,
TechBeamers