TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: Python File Handling Quiz for Experienced Programmers
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Python QuizzesPython Tutorials

Python File Handling Quiz for Experienced Programmers

Take the file handling quiz in Python designed for experienced programmers, featuring advanced questions that go beyond the basics.
Last updated: Feb 24, 2024 10:27 am
By Meenakshi Agarwal
Share
13 Min Read
Python file handling quiz part-2 for Experienced Programmers
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.

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)?

  1. Raw_input
  2. Input
  3. Read
  4. Scanner
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 2

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

  1. closed
  2. mode
  3. name
  4. rename
Hover here to view the answer!
Answer. 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.
Hover here to view the answer!
Answer. 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)
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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.
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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. []
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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.
Hover here to view the answer!
Answer. 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
Hover here to view the answer!
Answer. 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.
Hover here to view the answer!
Answer. 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.
Hover here to view the answer!
Answer. 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.

Hover here to view the answer!
Answer. 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.

Hover here to view the answer!
Answer. 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.

Hover here to view the answer!
Answer. 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.

Hover here to view the answer!
Answer. 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 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

You Might Also Like

How to Connect to PostgreSQL in Python

Generate Random IP Address (IPv4/IPv6) in Python

Python Remove Elements from a List

How to Use Extent Report in Python

10 Python Tricky Coding Exercises

TAGGED:Interactive Python Quiz
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article 7 Chrome Extensions to Find XPath 7 Best Chrome Extensions to Find XPath in Selenium
Next Article Python Functions Quiz Part-1 for Beginner Programmers Python Functions Quiz Part-1

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x