iGET

Python Programming - MCQ Practice Questions

Core Python MCQs — syntax, data types, OOP, libraries for placements & IT exams.

189 questions | 100% Free

Q.1Medium

Which of the following modes opens a file for both reading and writing without truncating the file, and raises an error if the file does not exist?

Q.2Medium

What will be the output of the following code? with open('test.txt', 'w') as f: f.write('Hello') f.write(' World') with open('test.txt', 'r') as f: print(f.read())

Q.3Medium

Which method reads all lines of a file and returns them as a list of strings, with each string ending in a newline character?

Q.4Medium

What does the seek(0) method do when called on an open file object?

Q.5Medium

What will be the output of the following code? f = open('data.txt', 'w') f.write('Line 1\nLine 2\nLine 3') f.close() f = open('data.txt', 'r') print(len(f.readlines())) f.close()

Q.6Medium

Which of the following is the correct way to open a file in binary write mode in Python?

Q.7Medium

What is the advantage of using the 'with' statement when working with files in Python?

Q.8Medium

What will the following code print? with open('sample.txt', 'w') as f: f.write('Python\nFile Handling') with open('sample.txt', 'r') as f: line = f.readline() print(line.strip())

Q.9Medium

What exception is raised when you try to open a file that does not exist using open('missing.txt', 'r')?

Q.10Medium

What will be the output of the following code? with open('test.txt', 'w') as f: f.write('Hello World') with open('test.txt', 'a') as f: f.write('!!!') with open('test.txt', 'r') as f: print(f.read())