tech beamers
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
    • Python
    • C
    • Java
    • MySQL
    • Linux
    • Web
    • Android
    • AngularJS
    • Playwright
    • Selenium
    • Agile
    • Testing
    • Automation
    • Best IDEs
    • How-To
    • Technology
    • Gaming
    • Branding
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
    • Python Interview
    • SQL Query Interview
    • SQL Exercises
    • Selenium Interview
    • Playwright Interview
    • QA Interview
    • Manual Testing
    • Rest API Interview
    • Linux Interview
    • CSharp Interview
    • Python Function Quiz
    • Python String Quiz
    • Python OOP Quiz
    • Python DSA Quiz
    • ISTQB Quiz
    • Selenium Quiz
    • Java Spring Quiz
    • Java Collection Quiz
    • JavaScript Quiz
    • Shell Scripting Quiz
  • ToolsHot
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
    • Online Python Compiler
    • Python Code Checker
    • Python Code Quality
    • Username Generator
    • Insta Password Generator
    • Google Password Generator
    • Free PDF Merger
    • QR Code Generator
    • Net Worth Calculator
tech beamers
Search
  • Viral Tips 🔥
  • Free CoursesTop
  • TutorialsNew
    • Python Tutorial
    • Python Examples
    • C Programming
    • Java Programming
    • MySQL Tutorial
    • Selenium Tutorial
    • Selenium Python
    • Playwright Python
    • Software Testing
    • Agile Concepts
    • Linux Concepts
    • HowTo Guides
    • Android Topics
    • AngularJS Guides
    • Learn Automation
    • Technology Guides
  • Interview & Quiz
    • SQL Interview
    • Testing Interview
    • Python Interview
    • Selenium Interview
    • C Sharp Interview
    • Java Interview
    • Web Development
    • PHP Interview
    • Python Quizzes
    • Java Quizzes
    • Selenium Quizzes
    • Testing Quizzes
    • HTML CSS Quiz
    • Shell Script Quizzes
  • ToolsHot
    • Python Online Compiler
    • Python Code Checker
    • C Online Compiler
    • Review Best IDEs
    • Random Letter Gen
    • Random Num Gen
Follow US
© TechBeamers. All Rights Reserved.
Python Quizzes

Python File Handling Quiz Part-2

Take the file handling quiz in Python designed for experienced programmers, featuring advanced questions that go beyond the basics.

Last updated: Mar 07, 2025 11:16 am
Meenakshi Agarwal
By Meenakshi Agarwal
No Comments
7 months ago
Share
13 Min Read
SHARE

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.

File Handling Quiz in Python for Experienced – Part II

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

Q-1. What are the two built-in functions in Python that read a line of text from Standard I/O (Keyboard)?

  1. Raw_input
  2. Input
  3. Read
  4. Scanner
Show Answer

Option – 1 and 2

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.
  1. Enter your input: Hello Viewers
    Received input is: Hello Viewers
  2. Enter your input: [x*5 for x in range(2,10,2)]
    Received input is : [10, 20, 30, 40]
  3. Both are correct
  4. None of the above
Show Answer

Option – 1

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.
  1. Enter your input: Hello Viewers
    Received input is: Hello Viewers
  2. Enter your input: [x*5 for x in range(2,10,2)]
    Received input is : [10, 20, 30, 40]
  3. Both are correct
  4. None of the above
Show Answer

Option – 2

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

  1. closed
  2. mode
  3. name
  4. rename
Show Answer

Option – 1, 2, and 3

Q-5. Which of the following statements correctly explains the function of the tell() method?

  1. It tells the current position within the file.
  2. It indicates that the next read or write will occur at that many bytes from the beginning of the file.
  3. It moves the current file position to a different location.
  4. It changes the file position only if allowed to do so else returns an error.
Show Answer

Option – 1 and 2

Q-6. Which of the following methods in Python can be used to check if a file exists?

  1. os.exists(file_path)
  2. file.exists(file_path)
  3. os.path.exists(file_path)
  4. path.exists(file_path)
Show Answer

Option – 3

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()
  1. TechBeamers
    Hello viewers!!
  2. Name of the file: myfile.txt
    TechBeamers
    Hello Viewers!!
  3. TechBeamers  Hello viewers!!
  4. Syntax Error
Show Answer

Option – 2

Q-8. Which statement correctly defines the function of Python’s truncate() call?

  1. It truncates the file to a size of 0.
  2. It deletes the content of the file.
  3. It truncates the file’s size and returns that content.
  4. None of the above
Show Answer

Option – 1

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

  1. Tech Beamers
  2. Tech
  3. Tech
    Beamers
  4. Error
Show Answer

Option – 1

Q-10. Which of the following statements correctly defines pickling in Python?

  1. It is a process to convert a Python object into a byte stream.
  2. It is a process to convert a byte stream to a Python object.
  3. It is done using two methods dump and load.
  4. Serialization is an alternate name for pickling.
Show Answer

Option – 1, 3 and 4

Q-11. Which is the correct syntax of the open() function in Python?

  1. file = open(file_name [, access_mode][, buffering])
  2. file object = open(file_name [, access_mode][, buffering])
  3. file object = open(file_name)
  4. None of the above
Show Answer

Option – 2

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()
  1. Name of the file:  myfile.txt
    The Read Line: [‘This is 1st line, This is 2nd line, This is 3rd line’]
  2. Name of the file:  myfile.txt
    The Read Line: [‘ This is 2nd line, This is 3rd line’]
  3. The Read Line: [ ‘This is 1st line’]
  4. Runtime Error
Show Answer

Option – 1

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()
  1. Runtime Error
  2. Hello World how are you today
  3. [‘Hello’, ‘World’, ‘how’, ‘are’, ‘you’, ‘today’]
  4. Hello
Show Answer

Option – 3

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)
  1. Runtime Error
  2. True
  3. False
  4. Hello world
Show Answer

Option – 2

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()
  1. Runtime Error
  2. [‘This is 1st line\n’, ‘ This is 2nd line\n’, ‘This is 3rd line’]
  3. [‘ This is 2nd line\n’, ‘This is 3rd line’]
  4. []
Show Answer

Option – 4

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()
  1. [‘ This is 1st line\n’]
  2. []
  3. IO Error
  4. None
Show Answer

Option – 3

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")
  1. This is the test file for exception handling!!
  2. Error: could not find a file or read data
  3. The content is written in the file successfully
  4. IO Error
Show Answer

Option – 2

Q-18. Which statement is true for the Python seek() method?

  1. It sets the file position to the end of the file.
  2. The position of the file pointer remains unchanged.
  3. An error occurs.
  4. The file pointer is set to the start of the file.
Show Answer

Option – 4

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)
  1. red
    yellow
    blue
  2. [‘red\n’, ‘yellow\n’, ‘blue\n’]
  3. ValueError: I/O operation on closed file.
  4. Compilation error
Show Answer

Option – 3

Q-20. Which statements are correct regarding the file access modes in Python?

  1. The ‘r+’ mode opens a file for both reading and writing. The file object points to its beginning.
  2. 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.
  3. 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.
  4. The ‘a’ mode opens a file for appending. The file pointer is at the end of the file if the file exists.
Show Answer

Option – All of the statements are correct.

Q-21. What does the with statement in Python file handling ensure?

  1. Automatic file closing after file operations are completed.
  2. File access permissions are checked before opening the file.
  3. File compression is applied during read and write operations.
  4. The file is opened in binary mode for efficient data handling.
Show Answer

Option – 1

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.

Show Answer

Option – 3

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.

Show Answer

Option – 2

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.

Show Answer

Option – 2

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.

Show Answer

Option – 3

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 Topics

  • 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, keep learning, and subscribe to our YouTube channel.

Thanks,
TechBeamers

Related

TAGGED:Interactive Python Quizprogrammingpython file handling
Share This Article
Whatsapp Whatsapp LinkedIn Reddit Copy Link
Meenakshi Agarwal Avatar
ByMeenakshi Agarwal
Follow:
Meenakshi Agarwal, Founder of TechBeamers.com, holds a BSc from Lucknow University and MCA from Banasthali University. With 10+ years as a Tech Lead at Aricent Technologies, she delivers expert Python, Java, Selenium, SQL, and AI tutorials, quizzes, and interview questions.
Previous Article Python File Handling Quiz Part-1 for Beginners Python File Handling Quiz Part-1
Next Article Python Functions Quiz Part-1 for Beginner Programmers Python Functions Quiz Part-1
Leave a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

List of Topics

Stay Connected

FacebookLike
XFollow
YoutubeSubscribe
LinkedInFollow

Continue Reading

  • Python Functions Quiz Part-1Oct 16
  • Python Functions Quiz Part-2Oct 16
  • Python Quiz: Classes and Objects Part 1Oct 22
  • Python Quiz: Classes and Objects Part 2Oct 25
  • Python Data Analysis Quiz for BeginnersApr 17
  • Python Quiz for Beginners Part-1Sep 24
  • Python Multithreading QuizNov 13
  • Python Programming Online Skill TestJan 13
  • Python Quiz for Beginners Part-2Feb 3
  • Python Entry-Level Quiz For DevelopersMar 2
View all →

RELATED TUTORIALS

How to Use Datatables in jQuery, Laravel, and Angular

How to Use DataTables in jQuery, Laravel, & Angular?

By Soumya Agarwal
7 months ago
50 Web Developer Interview Questions for Sure Success

Top Web Developer Interview Questions (2025)

By Meenakshi Agarwal
7 months ago
Python pass by reference vs pass by value

Pass by Reference vs Pass by Value in Python

By Meenakshi Agarwal
1 month ago
Python DSA Quiz - Part 5 Graphs & Graph Algorithms Test

Python DSA Quiz – Part 5 | Graphs & Graph Algorithms MCQ

By Meenakshi Agarwal
6 months ago
© TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use