Files Handling In Python — Part 2 MCQs with Explanations
Practice high-yield Programming & Software Development MCQs on Files Handling In Python — Part 2 with worked academic solutions for FPSC, PPSC, NTS, NITB, PTS.
Questions
38
Difficulty Level
Exam-Level
Target Exams
FPSC, PPSC, NTS...
Estimated Time
46 mins
Exam-Level FPSC PPSC NTS NITB PTS
Verified for Commission Syllabus • Oct 14, 2026
38
Verified Practice Questions
Interactive test mode with detailed academic worked solutions
Live Progress|0/38 answered
✓0 correct
✗0 wrong
0% score
🎉 Set complete! All 38 questions answered.
Question 1 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following is not a valid mode to open a file?
Correct Answer: Option B — rw
Detailed Academic Explanation:
rw is the correct answer to “Which of the following is not a valid mode to open a file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 2 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you copy the contents of one file to another in Python?
Correct Answer: Option A — By reading from one file and writing to another
Detailed Academic Explanation:
By reading from one file and writing to another is the correct answer to “How can you copy the contents of one file to another in Python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 3 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which one of the following is not attributes of file?
Correct Answer: Option C — rename
Detailed Academic Explanation:
rename is the correct answer to “Which one of the following is not attributes of file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 4 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the use of "w" in file handling?
Correct Answer: Option B — Write
Detailed Academic Explanation:
Write is the correct answer to “What is the use of "w" in file handling”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 5 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? str = raw_input("Enter your input: "); print "Received input is : ", str
Correct Answer: Option A — Enter your input: Hello Python Received input is : Hello Python
Detailed Academic Explanation:
Enter your input: Hello Python Received input is : Hello Python is the correct answer to “What will be the output of the following Python code? str = raw_input("Enter your input: "); print "Received input is : ", str”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 6 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line for index in range(5): line = fo.next() print "Line No %d - %s" % (index, line) # Close opened file fo.close()
Correct Answer: Option C — Displays Output
Detailed Academic Explanation:
Displays Output is the correct answer to “What will be the output of the following Python code? fo = open("foo.txt", "rw+") print "Name of the file: ", fo.name # Assuming file has following 5 lines # This is 1st line # This is 2nd line # This is 3rd line # This is 4th line # This is 5th line for index in range(5): line = fo.next() print "Line No %d - %s" % (index, line) # Close opened file fo.close()”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 7 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
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)
Correct Answer: Option A — True
Detailed Academic Explanation:
True is the correct answer to “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)”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 8 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How do you rename a file?
Correct Answer: Option B — os.rename(existing_name, new_name)
Detailed Academic Explanation:
os.rename(existing_name, new_name) is the correct answer to “How do you rename a file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 9 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you read the entire contents of a binary file in Python?
Correct Answer: Option A — By using the read() method on a file object
Detailed Academic Explanation:
By using the read() method on a file object is the correct answer to “How can you read the entire contents of a binary file in Python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 10 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? str = input("Enter your input: "); print "Received input is : ", str
Correct Answer: Option A — Enter your input: [x*5 for x in range(2,10,2)] Received input is : [x*5 for x in range(2,10,2)]
Detailed Academic Explanation:
Enter your input: [x*5 for x in range(2,10,2)] Received input is : [x*5 for x in range(2,10,2)] is the correct answer to “What will be the output of the following Python code? str = input("Enter your input: "); print "Received input is : ", str”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 11 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is unpickling?
Correct Answer: Option B — It is used for object deserialization
Detailed Academic Explanation:
It is used for object deserialization is the correct answer to “What is unpickling”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 12 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
The readlines() method returns . . . . . . . .
Correct Answer: Option B — a list of lines
Detailed Academic Explanation:
a list of lines is the correct answer to “The readlines() method returns . . . . . . . .”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 13 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following code: with open('data.txt', 'r') as file: lines = file.readlines() print(len(lines))
Correct Answer: Option B — An error will occur
Detailed Academic Explanation:
An error will occur is the correct answer to “What is the output of the following code: with open('data.txt', 'r') as file: lines = file.readlines() print(len(lines))”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 14 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
To read the next line of the file from a file object infile, we use . . . . . . . .
Correct Answer: Option C — infile.readline()
Detailed Academic Explanation:
infile.readline() is the correct answer to “To read the next line of the file from a file object infile, we use . . . . . . . .”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 15 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you rename a file in Python?
Correct Answer: Option A — By using the os.rename() function
Detailed Academic Explanation:
By using the os.rename() function is the correct answer to “How can you rename a file in Python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 16 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the pickling?
Correct Answer: Option A — It is used for object serialization
Detailed Academic Explanation:
It is used for object serialization is the correct answer to “What is the pickling”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 17 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following code: with open('data.txt', 'r') as file: content = file.read(10) print(content)
Correct Answer: Option A — The first 10 characters of data.txt
Detailed Academic Explanation:
The first 10 characters of data.txt is the correct answer to “What is the output of the following code: with open('data.txt', 'r') as file: content = file.read(10) print(content)”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 18 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How do you close a file object (fp)?
Correct Answer: Option C — fp.close()
Detailed Academic Explanation:
fp.close() is the correct answer to “How do you close a file object (fp)”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 19 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the use of seek() method in files?
Correct Answer: Option A — sets the file's current position at the offset
Detailed Academic Explanation:
sets the file's current position at the offset is the correct answer to “What is the use of seek() method in files”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 20 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the truncate() method in file handling?
Correct Answer: Option A — It resizes the file to the specified size
Detailed Academic Explanation:
It resizes the file to the specified size is the correct answer to “What is the purpose of the truncate() method in file handling”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 21 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
To read the remaining lines of the file from a file object infile, we use . . . . . . . .
Correct Answer: Option D — infile.readlines()
Detailed Academic Explanation:
infile.readlines() is the correct answer to “To read the remaining lines of the file from a file object infile, we use . . . . . . . .”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 22 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the a mode when opening a file in Python?
Correct Answer: Option A — Append mode
Detailed Academic Explanation:
Append mode is the correct answer to “What is the purpose of the a mode when opening a file in Python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 23 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which function is used to close a file in python?
Correct Answer: Option A — Close()
Detailed Academic Explanation:
Close() is the correct answer to “Which function is used to close a file in python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 24 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How do you get the current position within the file?
Correct Answer: Option B — fp.tell()
Detailed Academic Explanation:
fp.tell() is the correct answer to “How do you get the current position within the file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 25 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How do you delete a file?
Correct Answer: Option C — os.remove('file')
Detailed Academic Explanation:
os.remove('file') is the correct answer to “How do you delete a file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 26 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you remove a file in Python?
Correct Answer: Option A — By using the os.remove() function
Detailed Academic Explanation:
By using the os.remove() function is the correct answer to “How can you remove a file in Python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 27 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following is not a valid attribute of a file object (fp)?
Correct Answer: Option D — fp.size
Detailed Academic Explanation:
fp.size is the correct answer to “Which of the following is not a valid attribute of a file object (fp)”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 28 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the use of truncate() method in file?
Correct Answer: Option A — truncates the file size
Detailed Academic Explanation:
truncates the file size is the correct answer to “What is the use of truncate() method in file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 29 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How do you change the file position to an offset value from the start?
Correct Answer: Option A — fp.seek(offset, 0)
Detailed Academic Explanation:
fp.seek(offset, 0) is the correct answer to “How do you change the file position to an offset value from the start”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 30 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you open a file for both reading and writing in Python?
Correct Answer: Option A — By using the r+ mode
Detailed Academic Explanation:
By using the r+ mode is the correct answer to “How can you open a file for both reading and writing in Python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 31 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the difference between r+ and w+ modes?
Correct Answer: Option B — in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+
Detailed Academic Explanation:
in r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+ is the correct answer to “What is the difference between r+ and w+ modes”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 32 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the b mode when opening a file in Python?
Correct Answer: Option A — Binary mode
Detailed Academic Explanation:
Binary mode is the correct answer to “What is the purpose of the b mode when opening a file in Python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 33 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following code: with open('data.txt', 'r') as file: lines = file.read().splitlines() print(len(lines))
Correct Answer: Option A — The number of lines in data.txt
Detailed Academic Explanation:
The number of lines in data.txt is the correct answer to “What is the output of the following code: with open('data.txt', 'r') as file: lines = file.read().splitlines() print(len(lines))”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 34 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the current syntax of rename() a file?
Correct Answer: Option A — rename(current_file_name, new_file_name)
Detailed Academic Explanation:
rename(current_file_name, new_file_name) is the correct answer to “What is the current syntax of rename() a file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 35 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? fo = open("foo.txt", "wb") print "Name of the file: ", fo.name fo.flush() fo.close()
Correct Answer: Option D — Flushes the file when closing them
Detailed Academic Explanation:
Flushes the file when closing them is the correct answer to “What will be the output of the following Python code? fo = open("foo.txt", "wb") print "Name of the file: ", fo.name fo.flush() fo.close()”. This question is standard in competitive examination syllabi for Files Handling In Python.
file object = open(file_name [, access_mode][, buffering]) is the correct answer to “What is the correct syntax of open() function”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 37 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the readlines() method in file handling?
Correct Answer: Option A — It reads all lines of a file into a list
Detailed Academic Explanation:
It reads all lines of a file into a list is the correct answer to “What is the purpose of the readlines() method in file handling”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 38 of 38
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Is it possible to create a text file in python?
Correct Answer: Option A — Yes
Detailed Academic Explanation:
Yes is the correct answer to “Is it possible to create a text file in python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
✓Link copied to clipboard
Continue Your Practice
Advance Html — Part 1
Master the next curriculum module with 40 exam-standard MCQs.
💡Examiner's Advisory for Programming & Software Development
In competitive commission exams (FPSC, PPSC, KPPSC, SPSC, CSS, PMS), questions are frequently phrased with distractor options designed to catch rote learners. Whenever you answer an MCQ, read the Detailed Academic Explanation and Conceptual Anchor to understand why the remaining options are incorrect.