Text File MCQ Question Bank For Class 12

 

1. Which of the following command is used to open a file “c:\temp.txt” in read-mode only?

a) infile = open(“c:\temp.txt”, “r”)

b) infile = open(“c:\\temp.txt”, “r”) 

c) infile = open(file = “c:\temp.txt”, “r+”)

d) infile = open(file = “c:\\temp.txt”, “r+”)

b) infile = open(“c:\\temp.txt”, “r”) 

 

 

2. Which of the following command is used to open a file “c:\temp.txt” in write-mode only

a) outfile = open(“c:\temp.txt”, “w”)

b) outfile = open(file = “c:\temp.txt”, “w+”)

c) outfile = open(file = “c:\\temp.txt”, “w+”)

d) outfile = open(“c:\\temp.txt”, “w”) 

 

d) outfile = open(“c:\\temp.txt”, “w”) 

 

 

3. Which of the following command is used to open a file “c:\temp.txt” in append-mode?

a) outfile = open(“c:\temp.txt”, “a”) 

b) outfile = open(“c:\\temp.txt”, “rw”)

c) outfile = open(“c:\temp.txt”, “w+”)

d) outfile = open(“c:\\temp.txt”, “r+”)

a) outfile = open(“c:\temp.txt”, “a”) 

 

 

4. Which of the following commands can be used to read “n” number of characters from a file using the file object <file>?

a) read(n)

b) n = file.read()

c) file.readline(n)

d) file.readlines()

a) read(n)

 

 

5. Which of the following commands can be used to read the entire contents of a file as a string using the file object <tmpfile>?

a) read(n)

b) read()

c) readline()

d) readlines()

b) read()

 

 

6. Which of the following commands can be used to read the next line in a file using the file object <tmpfile>?

a) read(n)

b) tmpfile.read()

c) readline() 

d) tmpfile.readlines()

c) readline()

 

 

7. Which of the following commands can be used to read the remaining lines in a file using the file object <tmpfile>?

a) read(n)

b) read()

c) readline()

d) readlines()

c) readline()

 

 

8. What does the <readlines()> method returns?

a) Str

b) a list of lines

c) list of single characters

d) list of integers

b) a list of lines 

 

 

 

#TRENDING

Binary File MCQs for Class 12 Term 1

CSV File MCQs for Class 12 Term 1

 

 

MCQ Text file, Binary file, CSV file, relative and absolute paths

9. How many types of files are there in python?

a) 1

b) 2

c) 3

d) 4

b) 2 

 

 

10. The files that consists of human readable characters

a) Text file

b) Binary file

c) Both Text and Binary file

d) None of the above.

a) Text file

 

 

11. Each line of a text file is terminated by a special character, called?

a) End of file

b) End of byte

c) End of line 

d) All the above

c) End of line

 

 

12. Trying to open a binary file using a text editor will show: 

a) Garbage values 

b) ASCII values

c) Binary character

d) Unicodes

a) Garbage values 

 

 

13. In file handling, what does this terms means “r, a”?

a) read, append 

b) append, read

c) write, append

d) none of the mentioned

a) read, append

 

 

14. Which function is used to read single line from file?

a) readline() 

b) readlines()

c) readstatement()

d) readfullline()

a) readline()

 

 

15. Which function is used to close a file in python?

a) close() 

b) stop()

c) end()

d) closefile()

a) close()

 

 

16. A file maintains a __________ which tells the current position in the file where writing or reading will take place.

a) line

b) file pointer 

c) list

d)order

b) file pointer 

 

 

17. Which of the following file modes opens a file for appending and reading in a binary file and moves the files pointer at the end of the file if the file already exists or creates a new file?

a) a

b) a+

c) ab+ 

d) ab

c) ab+ 

 

 

18. Which method of pickle module is used to write onto a binary file?

a) dump() 

b) load()

c) All of the above

d) None of the above

a) dump()

 

 

Case Study Questions on Text File 

19. Aishwarya is running her own boutique business. She wants to store data of all orders permanently and fast processing of data through her boutique software. Suggest her to choose the appropriate technique among the following.

a) She can use Python Dictionaries with Text files.

b) She can use Python Dictionaries with Binary file concept. 

c) She can use Python Lists without the Binary files concept.

d) She can use Python Dictionaries without the Binary file concept.

b) She can use Python Dictionaries with Binary file concept. 

 

 

 

20. A programmer has confusion in understanding the behaviour of opening a file in “w” mode. Clear his/her confusion, by suggesting the correct option among the given below. The behaviour of “w” mode is

a) Opening fails if the file already exists already.

b) Opening fails if the file does not exist already.

c) Opening will be succeeded if file exists with data and keeps the data intact.

d) Opening will be succeeded, if the file exists replaces the contents, do not exist, creates a new file.

d) Opening will be succeeded, if the file exists replaces the contents, do not exist, creates a new file.

 

 

21. Aditya is a class 12 student like you. He is practicing text file programming. He has a text file named names.txt. He wants to display all the names contained in the text file. He has written one of the following codes and succeeded in getting the output. Guess which among the following option might have given the correct output.

a) names = open(“names.txt”, “r”)

for line in names:

print(names)

 

c) names = open(“names.txt”, “r”)

for line in names:

print(“line”)

 

b) names = open(“names.txt”, “r”)

for line in names:

print(line) 

 

d) names = open(“names.txt”, “r”)

for names in line:

print(line)

b) names = open(“names.txt”, “r”)

for line in names:

print(line)

 

 

22. A Student’s windows O/S got corrupted. He is trying to access his files through Command Prompt, but unable to find out all his Binary data files pertaining to his project. Help him to find out all his binary data files by suggesting the suitable extension name among the following.

a) .txt

b) .bin

c).dat 

d) .com

c).dat

 

 

23. Given the following directory structure. Assume that the CWD is in the root folder where animals directory resides. What is the relative path to the feline folder?

path

a) C:/animals/feline

b) animals/feline

c) feline/animals

d) None of the above.

b) animals/feline

 

 

24. Given the directory structure in Q 23. Assume that the CWD is in the feline folder. What is the relative path to the file bears.gif?

a) C:/animals/ursine

b) animals/ursine

c) ..ursine/bears.gif 

d) None of the above.

c) ..ursine/bears.gif

 

 

25. Given the directory structure in Q 23. Assume that the CWD is in the root folder where animals directory resides. What is the absolute path to bears.gif?

a) C:/animals/ursine

b) ../animals/feline

c) /animals/ursine/bears.gif 

d) None of the above.

c) /animals/ursine/bears.gif

 

 

26. Given the directory structure in Q 23. Assume that the CWD is in the feline folder what is the relative path to lions.gif?

a) C:/animals/ursine

b) lions.gif 

c) /animals/ursine/bears.gif

d) None of the above.

b) lions.gif 

 

 

27. Master Adithya could able to read contents of a text file part by part. Out of the following which option he would have been used?

a) read()

b) readlines()

c) readline() 

d) readfullline()

c) readline() 

 

 

28. In a program, there is a need of reading whole content of a textfile in a single shot and store it in the form of list. Suggest the best option among the following

a) read()

b) readline()

c) readlines() 

d) None of the above

c) readlines()

 

 

Text file: opening a text file, text file open modes (r, r+, w, w+, a, a+),  closing a text file, opening a file using with clause

 

29. What is the default mode when the file is opened using the open() method ?

a) Write

b) Read 

c) Write and read

d) Read and write

b) Read 

 

 

30. The function read() is used to

a) Read the entire content of the file 

b) Read the entire content of the file in the form of list

c) Read the content of the file line by line

d) Read each string of the content of the file

a) Read the entire content of the file

 

 

31. Which of the following statements is true with respect to closing a file

a) It is mandatory to close a text file which is opened

b) Python automatically closes a file

c) Python automatically closes a file if the reference object of the file is allocated to another 

d) None of the above

c) Python automatically closes a file if the reference object of the file is allocated to another

 

 

32. Which of the following statements is true?

a) Python doesn’t write data to the file until you close the file using the close() method

b) If the file is opened for writing and is not closed, data is not written into the target file. 

c) Python can write data to the file even if you do not close the file

d) None of the above

b) If the file is opened for writing and is not closed, data is not written into the target file.

 

 

33. The mode “w+” is used to open the file for

a) Read and write a text file

b) Read and write a binary file

c) Write and Read a text file 

d) Write and Read a binary file

c) Write and Read a text file

 

 

34. When a file is opened in “w” mode, the file pointer is placed at

a) the beginning of the file. 

b) The end of the file.

c) the middle of the file.

d) the current position of the file.

a) the beginning of the file.

 

 

35. When a file is opened in “a” mode, the file pointer is placed at

a) the beginning of the file.

b)The end of the file. 

c) the middle of the file.

d) the current position of the file.

b)The end of the file.

 

 

36. (A) : If a file is opened using the “with” statement, you get better syntax and exceptions handling.

(B): When a file is opened using the “with” statement, it need not be closed using the close() function.

In the above two statements, which of the following is true?

a) Both A and B are Wrong

b) A is Wrong, but B is right.

c) A is right , but B is Wrong

d) Both A and B are right. 

d) Both A and B are right. 

 

37. In r+ mode, if we write the file directly, it will

a) overwrite the beginning content 

b) continue reading from the previous content

c) continue writing from the previous content

d) work like the append mode

a) overwrite the beginning content 

 

 

38. When the file content has to be modified, we can use the ____________ mode

a) r 

b) w

c) a

d) r+

a) r 

 

 

39. Rajitha, during Practical Examination of Computer Science, has been assigned an incomplete search() function to search in a text file “CAMP.txt”. The file “CAMP.txt” is created by his Teacher and the following information is known about the file

i) File contains details of camp describing events of an adventure camp in text format

ii) File contains details of adventure activities like caving, trekking, paragliding, rafting and rock climbing Rajitha has been assigned the task to complete the code and print the number of the word trekking

def search():

f = open(“CAMP.txt”,____) #Statement-1

A=____________________ #Statement-2

ct=0

for x in A:

p=x.split()

if p==”trekking”:

ct+=1

print(ct) _______________________# Statement-3

 

39.1. In which mode Rajitha should open the file in Statement-1?

a) r 

b) r+

c) rb

d) wb

a) r 

 

 

39.2. Name the function that can be used by Rajitha to read the content of the file in statement -2.

a) f.read( )

b) f.readline ()

c) f.readlines( ) 

d)f.readl()

c) f.readlines( )

 

 

39.3.Which statement should Rajitha use in Statement 3 to close the file.

a) file.close()

b) close(file)

c) f.close() 

d) close()

c) f.close() 

 

40. Rahul is assigned the task of storing the information of his class mates as student records in a text file “Stu2021.txt”.He wants to create a file to write the records. Which of the following is not a valid mode to open a file for writing the data?

a) w

b) r+

c) r 

d) w+

c) r 

 

41. Rohit of class 12 is asked by his Computer Science teacher to count the number of “the” and “and” in a text file “Book.txt”. He was able to do the task to some extent, but was confused in some areas. Help him out to complete the task

def displaytheand():

num=0 _________ #Statement 1

N=f.read()

M=N.split()

for x in M:

if x==”the” or x== “and”:

print(x) num=num+1 __________ #Statement 2

print(“Count of the/My in and:”,num)

 

41.1. Which of the following is the correct one to be used in the place of statement 1

a) f=open(“Book.txt”,”w”)

b) f=open(“Book.txt”,”r”) 

c) f=open(“Book.txt”,”a”)

d) F=open(“Book.txt”,”r”)

b) f=open(“Book.txt”,”r”) 

 

 

41.2. Identify the correct statement that may be used in the place of statement 2

a) f.close() 

b) f.close(“book.txt”)

c) close()

d) None of the above

a) f.close() 

 

 

42. Ajay is studying in an Engineering College in CSE branch. His sister, a class 12 student of Computer Science comes to him one day asking him the difference between r+ and w+ modes of a file. What is the correct answer Ajay would give to his sister?

a) No difference between r+ and w+

b) In r+ mode, the file is created if it does not exist and erases the content if the file already exists; w+ raises an error if the file does not exist

c) In w+ mode, the file is created if it does not exist and erases the content if the file already exists; r+ raises an error if the file does not exist 

d) Depends on the operating system

c) In w+ mode, the file is created if it does not exist and erases the content if the file already exists; r+ raises an error if the file does not exist 

 

 

43. Aparajitha joined an MNC company in Bangalore as a Python Programmer. Her task is to handle the data available for the company in the form of text files and perform the search operations based on specific criteria. Now she is asked to count the number of words in the file which start with “a” or “m” (both upper and lower cases) in a text file “Passion.txt”. She needs your help as she is stuck up with some statements. Please help her out to complete the task

def filecreate():

f=open(“Passion.txt”,”r”) #Statement 1

f.write() f.close() # Statement 2

def count_A_M():

f=open(“Passion.txt”,”r”) # Statement 3

A,M=0,0

r=f.read()

for x in r:

if x[0]==”A” or x[0]==”a” :

A=A+1 25 elif x[0]==”M” or x[0]==”m”: M=M+1

f.close()

print(“A or a: “,A)

print(“M or m: “,M)

 

43.1) Choose the correct option to be used as statement 1

a) f=open(“Passion.txt”,”r”)

b) f=open(“Passion.txt”,”r+”)

c) f=open(“Passion.txt”,”w”) 

d) f=open(“Passion.txt”,”a”)

c) f=open(“Passion.txt”,”w”)

 

 

43.2) Which of the following options can be used as statement 3

a) f=open(“Passion.txt”,”r”) 

b) f=open(“Passion.txt”,”w”)

c) f=open(“Passion.txt”,”a”)

d) f=open(“Passion.txt”,”rt”) 

Option a) and d) both are correct 

 

 

43. 3) Choose the correct option to be used as statement 2

a) F.close()

b) f.close() 

c) F.close(“passion.txt”)

d) No statement is required

b) f.close()

 

 

44. Which of the following statements is/are true?

a) When you open a file for reading, if the file does not exist, an error occurs.

b) When you open a file for writing, if the file does not exist, a new file is created.

c) When you open a file for writing, if the file exists, the existing file is overwritten with the new file. (

d) All of the above. 

d) All of the above. 

 

 

45. Which of the following statements is not True

a) File method close() closes the opened file.

b) Python automatically closes a file when the reference object of a file is reassigned to another file.

c) close() method returns a value which ensures the termination of the file stream 

d) Calling close( ) method more than once is allowed.

c) close() method returns a value which ensures the termination of the file stream

 

 

46. What is the expected output of the given code?

f = None

for i in range (5):

with open(“data.txt”, “w”) as f:

if i > 2:

break

print(f.closed)

a) True 

b) False

c) None

d) Error

a) True 

 

 

47. Which of the following statements is true with respect to the files in Python?

a) File can be opened with a file handle using open method without any arguments

b) File can be opened with a file handle using open method with one argument to read the file 

c) File can be opened with a file handle using open method with one argument to write the file

d) File can be opened with a file handle using open method with one argument to append the file

b) File can be opened with a file handle using open method with one argument to read the file ✓

 

 

48. To open a file only for reading which of the following statement cannot be used :

a) f = open(“PYTHON.txt”)

b) f = open(“PYTHON.txt”, “rt”)

c) f = open(“PYTHON.txt”, “r”)

d) f = open(“PYTHON.txt”,”r+”) 

d) f = open(“PYTHON.txt”,”r+”) 

 

 

Copywrite © 2020-2024, CBSE Python,
All Rights Reserved