Files Handling In Python — Part 1 MCQs with Explanations
Practice high-yield Programming & Software Development MCQs on Files Handling In Python — Part 1 with worked academic solutions for FPSC, PPSC, NTS, NITB, PTS.
Questions
40
Difficulty Level
Exam-Level
Target Exams
FPSC, PPSC, NTS...
Estimated Time
48 mins
Exam-Level FPSC PPSC NTS NITB PTS
Verified for Commission Syllabus • Oct 14, 2026
40
Verified Practice Questions
Interactive test mode with detailed academic worked solutions
Live Progress|0/40 answered
✓0 correct
✗0 wrong
0% score
🎉 Set complete! All 40 questions answered.
Question 1 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How do you open a file named data.txt in read mode in Python?
Correct Answer: Option A — file = open('data.txt', 'r')
Detailed Academic Explanation:
file = open('data.txt', 'r') is the correct answer to “How do you open a file named data.txt in read mode in Python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 2 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the with statement in file handling?
Correct Answer: Option A — It ensures that the file is properly closed after usage
Detailed Academic Explanation:
It ensures that the file is properly closed after usage is the correct answer to “What is the purpose of the with statement in file handling”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 3 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you write data to a file in Python?
Correct Answer: Option A — By using the write() method on a file object
Detailed Academic Explanation:
By using the write() method on a file object is the correct answer to “How can you write data to 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 4 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What does the read() method of a file object do?
Correct Answer: Option A — It reads the entire contents of the file
Detailed Academic Explanation:
It reads the entire contents of the file is the correct answer to “What does the read() method of a file object do”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 5 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you read a single line from a file in Python?
Correct Answer: Option A — By using the readline() method on a file object
Detailed Academic Explanation:
By using the readline() method on a file object is the correct answer to “How can you read a single line from 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 6 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the seek() method in file handling?
Correct Answer: Option A — It changes the file position to a specified offset
Detailed Academic Explanation:
It changes the file position to a specified offset is the correct answer to “What is the purpose of the seek() 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 7 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you read the entire contents of a file line by line in Python?
Correct Answer: Option B — By using the readlines() method on a file object
Detailed Academic Explanation:
By using the readlines() method on a file object is the correct answer to “How can you read the entire contents of a file line by line in Python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 8 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following code: with open('data.txt', 'a') as file: file.write('Appended line.') with open('data.txt', 'r') as file: content = file.read() print(content)
Correct Answer: Option D — file object
Detailed Academic Explanation:
file object is the correct answer to “What is the output of the following code: with open('data.txt', 'a') as file: file.write('Appended line.') with open('data.txt', 'r') as file: content = file.read() print(content)”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 9 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you create a new file in Python if it doesn't exist?
Correct Answer: Option A — By using the 'x' mode when opening the file
Detailed Academic Explanation:
By using the 'x' mode when opening the file is the correct answer to “How can you create a new file in Python if it doesn't exist”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 10 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
To open a file c:\scores.txt for reading, we use . . . . . . . .
Correct Answer: Option B — infile = open(“c:\\scores.txt”, “r”)
Detailed Academic Explanation:
infile = open(“c:\\scores.txt”, “r”) is the correct answer to “To open a file c:\scores.txt for reading, we use . . . . . . . .”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 11 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
To open a file c:\scores.txt for appending data, we use . . . . . . . .
Correct Answer: Option A — outfile = open(“c:\\scores.txt”, “a”)
Detailed Academic Explanation:
outfile = open(“c:\\scores.txt”, “a”) is the correct answer to “To open a file c:\scores.txt for appending data, we use . . . . . . . .”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 12 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you close a file in Python?
Correct Answer: Option A — By using the close() method on a file object
Detailed Academic Explanation:
By using the close() method on a file object is the correct answer to “How can you 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 13 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the tell() method in file handling?
Correct Answer: Option A — It returns the current file position
Detailed Academic Explanation:
It returns the current file position is the correct answer to “What is the purpose of the tell() 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 14 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
To open a file c:\scores.txt for writing, we use . . . . . . . .
Correct Answer: Option B — outfile = open(“c:\\scores.txt”, “w”)
Detailed Academic Explanation:
outfile = open(“c:\\scores.txt”, “w”) is the correct answer to “To open a file c:\scores.txt for writing, 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 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the purpose of the write() method in file handling?
Correct Answer: Option A — It writes data to the file
Detailed Academic Explanation:
It writes data to the file is the correct answer to “What is the purpose of the write() 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 16 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
To read two characters from a file object infile, we use . . . . . . . .
Correct Answer: Option A — infile.read(2)
Detailed Academic Explanation:
infile.read(2) is the correct answer to “To read two characters 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 17 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following code: file = open('data.txt', 'r') content = file.read() print(content)
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: file = open('data.txt', 'r') content = file.read() 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 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the output of the following code: with open('data.txt', 'w') as file: file.write('Hello, world!')
Correct Answer: Option C — The file data.txt will be empty
Detailed Academic Explanation:
The file data.txt will be empty is the correct answer to “What is the output of the following code: with open('data.txt', 'w') as file: file.write('Hello, world!')”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 19 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following code: with open('data.txt', 'r') as file: lines = file.readlines() 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 will be 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 20 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Correct syntax of file.writelines() is?
Correct Answer: Option C — fileObject.writelines(sequence)
Detailed Academic Explanation:
fileObject.writelines(sequence) is the correct answer to “Correct syntax of file.writelines() is”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 21 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which is/are the basic I/O connections in file?
Correct Answer: Option D — All of the mentioned
Detailed Academic Explanation:
All of the mentioned is the correct answer to “Which is/are the basic I/O connections in file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 22 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the use of "a" in file handling?
Correct Answer: Option C — Append
Detailed Academic Explanation:
Append is the correct answer to “What is the use of "a" in file handling”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 23 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which are the two built-in functions to read a line of text from standard input, which by default comes from the keyboard?
Correct Answer: Option A — Raw_input & Input
Detailed Academic Explanation:
Raw_input & Input is the correct answer to “Which are the two built-in functions to read a line of text from standard input, which by default comes from the keyboard”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 24 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you check the current position in a file in Python?
Correct Answer: Option A — By using the tell() method on a file object
Detailed Academic Explanation:
By using the tell() method on a file object is the correct answer to “How can you check the current position in 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 25 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How can you check if a file exists in Python before opening it?
Correct Answer: Option A — By using the os.path.exists() function
Detailed Academic Explanation:
By using the os.path.exists() function is the correct answer to “How can you check if a file exists in Python before opening it”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 26 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which function is used to read single line from file?
Correct Answer: Option B — Readlines()
Detailed Academic Explanation:
Readlines() is the correct answer to “Which function is used to read single line from file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 27 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following are the modes of both writing and reading in binary format in file?
Correct Answer: Option A — wb+
Detailed Academic Explanation:
wb+ is the correct answer to “Which of the following are the modes of both writing and reading in binary format in file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 28 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
To read the entire remaining contents of the file as a string from a file object infile, we use . . . . . . . .
Correct Answer: Option B — infile.read()
Detailed Academic Explanation:
infile.read() is the correct answer to “To read the entire remaining contents of the file as a string 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 29 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which function is used to read all the characters?
Correct Answer: Option A — Read()
Detailed Academic Explanation:
Read() is the correct answer to “Which function is used to read all the characters”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 30 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the current syntax of remove() a file?
Correct Answer: Option A — remove(file_name)
Detailed Academic Explanation:
remove(file_name) is the correct answer to “What is the current syntax of remove() a file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 31 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which function is used to write all the characters?
Correct Answer: Option A — write()
Detailed Academic Explanation:
write() is the correct answer to “Which function is used to write all the characters”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 32 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What will be the output of the following Python code? (If entered name is examveda) import sys print 'Enter your name: ', name = '' while True: c = sys.stdin.read(1) if c == '\n': break name = name + c print 'Your name is:', name
Correct Answer: Option A — examveda
Detailed Academic Explanation:
examveda is the correct answer to “What will be the output of the following Python code? (If entered name is examveda) import sys print 'Enter your name: ', name = '' while True: c = sys.stdin.read(1) if c == '\n': break name = name + c print 'Your name is:', name”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 33 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What happens if no arguments are passed to the seek function?
Correct Answer: Option D — error
Detailed Academic Explanation:
error is the correct answer to “What happens if no arguments are passed to the seek function”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 34 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Correct syntax of file.readlines() is?
Correct Answer: Option A — fileObject.readlines( sizehint )
Detailed Academic Explanation:
fileObject.readlines( sizehint ) is the correct answer to “Correct syntax of file.readlines() is”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 35 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which of the following mode will refer to binary data?
Correct Answer: Option A — r
Detailed Academic Explanation:
r is the correct answer to “Which of the following mode will refer to binary data”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 36 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
How do you get the name of a file from a file object (fp)?
Correct Answer: Option A — fp.name
Detailed Academic Explanation:
fp.name is the correct answer to “How do you get the name of a file from 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 37 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
In file handling, what does this terms means "r, a"?
Correct Answer: Option A — read, append
Detailed Academic Explanation:
read, append is the correct answer to “In file handling, what does this terms means "r, a"”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 38 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What is the use of tell() method in python?
Correct Answer: Option A — tells you the current position within the file
Detailed Academic Explanation:
tells you the current position within the file is the correct answer to “What is the use of tell() method in python”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 39 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
Which function is used to write a list of string in a file?
Correct Answer: Option A — writeline()
Detailed Academic Explanation:
writeline() is the correct answer to “Which function is used to write a list of string in a file”. This question is standard in competitive examination syllabi for Files Handling In Python.
💡Key Conceptual Anchor:Files Handling In Python
Question 40 of 40
★ FPSC
★ PPSC
★ FPSC
★ PPSC
What does the w mode indicate when opening a file in Python?
Correct Answer: Option A — Write mode
Detailed Academic Explanation:
Write mode is the correct answer to “What does the w mode indicate 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
✓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.