0% found this document useful (0 votes)
6 views5 pages

File Handling Python MCQ

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
6 views5 pages

File Handling Python MCQ

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

File Handling Python MCQ

1. _______ is a named location on a secondary storage media where data are permanently
stored
a. Memory b. Folder c. File d. Drive
c.File

2. Which of the following is data file?


a. Text file b. Binary file
c. Both (a) and (b) d. None of these
c. Both (a) and (b)

3. (i) Computer stores every file in binary format.


(ii) both text and binary files are human readable.
a. (i) True, (ii) False b. (i) False, (ii) True
c. Both are True d. Both are False
a. (i) True, (ii) False

3. Which is not true about text files?


(i) Files with extensions like .py, .csv, .docx are considered as text files.
(ii) Text file content are stored in ASCII, UNICODE or any other encoding scheme
(iii) Text files are not human readable
a. (i), (ii) True b. (i), (iii) True
c. All True d. Only (i) True
a. (i), (ii) True

4. EOL stands for:


a. End of list b. End of line
c. End of Length d. End of Level
b. End of line

5. Default EOL character of text file in python is


a. Comma (,) b. Tab (\t)
c. Space (‘ ‘) d. Newline (\n)
d. Newline (\n)

6. “ The default EOL character in Python is ‘\n’, however other characters like comma,
space can also be used to indicate EOL. “
a. True b. False
c. Depends upon types of text files d. None of these
b. False

7. An image is a type of
a. Text file b. Binary file
c. Executable file d. Csv files
b. Binary file

8. (i) A python program can read only binary files


(ii) Changing a single bit can corrupt a binary file completely
a. (i) true, (ii) false b. (i) false, (ii) true
c. Both are true d. Both are false
b. (i) false, (ii) true

9. Document created in notepad is an example of:


a. Text file b. Binary file
c. Csv file d. Log file
a. Text file

10. File with extension of .mp4 is an example of


a. Text file b. Binary file
c. Executable file d. OS
b. Binary file

11. Which of the following is true about binary files


a. Binary files are human readable
b. Binary file uses ASCII, UNICODE like encoding scheme to store its content
c. Processing of binary file is slow as compared to text file.
d. To read and write the contents o a binary file specific software is required
d. To read and write the contents o a binary file specific software is required

12. When you try to open a binary file in MS-Word, it will show
a. Actual content b. Garbage values
c. Will not be opened d. Binary digits
b. Garbage values

13. Which of the following extension is not a binary file?


a. .py b. .exe c. .mp3 d. .jpeg
a. .py

14. Which of the following extension does not represent a text file?
a. .txt b. .docx c. .csv d. .py
b. docx

15. ___________ can be read by humans.


a. Text files b. Binary files
c. Both (a) and (b) d. None of these
a. Text files

16. Which of the following is correct syntax to open a file?


a. file_object = open(“file_name”,”access_mode”)
b. file_object = open(“file_name”)
c. both (a) and (b)
d. None of these
c. Both (a) and (b)

17. Which of the following is not an appropriate file access mode?


a. <a> b. <r+> c. <w> d. <wr+>
d. <wr+>

18. What will happen if you execute following code:


myobject = open(“myfile.txt”)
a. opens myfile.txt in write mode
b. opens myfile.txt in read mode
c. open myfile.txt in both read and write mode
d. open myfile.txt in append mode
b. opens myfile.txt in read mode

19. What will happen if you execute following code:


myobject = open(“myfile.txt”)
a. opens myfile.txt in text and write mode
b. opens myfile.txt in text and read mode
c. open myfile.txt in binary and write mode
d. open myfile.txt in binary and read mode.
b. opens myfile.txt in text and read mode

20. Raju wants to open ‘exp.txt’ file in read, write and binary mode. Help him in writing
correct code
a. Myobject = open(‘exp.txt’, ‘ab+’) b. Myobject = open(‘exp.txt’, ‘wb+’)
c. Myobject = open(‘exp.txt’, ‘r+’) d. Myobject = open(‘exp.txt’, ‘w+’)
b. Myobject = open(‘exp.txt’, ‘wb+’)

21. If you want to add new line of text in ‘sample.txt’, than which file open mode you would
select?
a. a b. a+ c. w d. both (a) and (b)
d. both (a) and (b)

22. Which of the following is not true regarding ‘w’ file open mode?
a. open files in write mode
b. it replaces old content with new one in existing file
c. if file does not exists, than produces ‘FileNotFound Error’
d. All are true
c. if file does not exists, than produces ‘FileNotFound Error’
23. Which of the following is not true about ‘a’ file open mode?
a. Open files in writing mode
b. If file does not exist, than it creates a new file
c. It places cursor at the beginning of file.
d. It merges new content with existing content of file.
c. It places cursor at the beginning of file.

24. It breaks the link between file object and data file stored in disk
a. EOF b. Close() c. Break d. Dump()
b. Close()

25. The difference between r+ and w+ is:


a. No difference as both are used to read and write content
b. Depends upon type of file
c. In r+ mode cursor is initially placed at the beginning of the file and for w+ cursor is
placed at the end
d. In r+ cursor is initially placed at the end of file and for w+ cursor is placed at the
beginning

26. A .pdf file is


a. Text file b. Binary file
c. Executable file d. Csv file
b. Binary file

27. For writing to a file we can use


a. Write() b. Writeline()
c. Writelines() d. Both (a) and (c)
d. Both (a) and (c)

28. Method which returns total number of characters after writing it to the text file
a. Write() b. Writelines() c. Both d. None
a. Write()

29. Consider the following code:


fileobject = open(‘sample.txt”,”w”)
fileobject.write(“Techtipnow computers\n”)
fileobject.close()
When you execute this, what would be the output?
a. No output b. 20 c. 21 d. Techtipnow computers
a. No output

30. Suhail wants to write a number 23 into a text file exp.txt. help him in writing suitable
code. (assume that exp.txt is already opened and ‘fileobj’ is file handle)
a. Fileobj.write(23) b. Fileobj.write(‘23’)
c. Both are correct d. None of these
b. Fileobj.write(‘23’)

31. Jorawar wants to add multiple line string as given below in sample.txt.
Techtipnow computer Education and Programming Help him in writing suitable code.
a. Fileobj = open(‘sample.txt’,’w’)
x = [‘techtipnow computer\n’,’education and\n’,’programming\n’]
fileobj.writelines(x)
b. Fileobj = open(‘sample.txt’,’w’)
x = (‘techtipnow computer\n’,’education and\n’,’programming\n’)
fileobj.writelines(x)
c. Both (a) and (b)
d. None of these
c. Both (a) and (b)

32. Gabbar wants to append content in a text file without closing file object. What method
he can choose to do so?
a. Exit() b. Clean() c. Clear() d. Flush()
d. Flush()
33. If you want to read a text file, than it must be opened in
a. r+ mode b. w+ mode c. a+ mode d. All of the above
d. All of the above

34. Display the output of following code:


Fileobj = open(‘sample.txt’,’w’)
x = [‘techtipnow\n’,’education\n’,’programming\n’]
fileobj.writelines(x)
Fileobj.readline()
a. ‘techtipnow\n’
b. ‘techtipnow\neducation\nprogramming\n’
c. [‘techtipnow\n’,’education\n’,’programming\n’]
d. (‘techtipnow\n’,’education\n’,’programming\n’)
a. ‘techtipnow\n’

35. This method reads one complete line from a file where each line terminates with a
newline (\n) character.
a. Read() b. Readline()
c. Readlines() d. Traverse()
b. Readline()

36. To read entire content of a file one can use


a. Read() b. Readline()
c. Readlines() d. Both (a) and (c)
d. Both (a) and (c)

37. readlines() returns all the lines as a _____ of string.


a. List b. Tuple
c. Dictionary d. Both (a) and (b)
a. List

38. predict the output of following code:


filo = open(‘sample.txt’,’w+’)
f.write(‘techtipnow\ncomputers’)
f.seek(0)
r = filo.read(7)
print(r)
a. techti b. techtip
c. techtipn d. none of these
b. techtip

39. predict the output of given code(assume exp.txt contains ‘techtipnow\n’ as data :
fh = open(‘exp.txt’,’r’)
s = len(fh.read())
print(s)
a. 0 b. 10 c. 11 d. No output
d. No output

40. ____ method is used to position the file object at a particular position in a data file
a. Seek() b. Tell() c. Dump() d. Move()
a. Seek()

41. ______ method is used to read data from a binary file


a. Dump() b. Load() c. Write() d. Seek()
b. Load()

42. ______ method is used to convert python objects for writing data in a binary file.
a. Dump() b. Load() c. Seek() d. Write()
a. Dump()

43. Process of transforming data or object in memory (RAM) to a stream of bytes that in a
binary file can be than stored in disk is called
a. Pickling b. Serialization
c. Dumping d. Both (a) and (b)
d. Both (a) and (b)
44. To add and display data for a binary file we can use _________ module
a. Pickle b. Serialize
c. Pandas d. Panther
a. Pickle

45. Predict the output of given code:


fileobject = open(“sample.txt”,”r+”)
a. Opens sample.txt in binary and read only mode
b. Opens sample.txt in text and read only mode
c. Opens sample.txt in binary, read and write mode
d. Opens sample.txt in text, read and write mode
d. Opens sample.txt in text, read and write mode

You might also like