File Handling Python MCQ

Get latest and Best Python File Handling MCQ with answers for those preparing for Computer Science, Information Technology, Informatics Practices, Python Programming, CUET etc. compiled by well experienced and qualified IT experts.

  1. _______ is a named location on a secondary storage media where data are permanently stored

    a. Memory
    b. Folder
    c. File
    d. Drive

Show Answer

c.File

  1. Which of the following is data file?

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

Show Answer

c. Both (a) and (b)

  1. (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

Show Answer

a. (i) True, (ii) False

  1. 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

Show Answer

a. (i), (ii) True

  1. EOL stands for:

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

Show Answer

b. End of line

  1. Default EOL character of text file in python is

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

Show Answer

d. Newline (\n)

  1. “ 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

Show Answer

b. False

  1. An image is a type of

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

Show Answer

b. Binary file

  1. (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

Show Answer

b. (i) false, (ii) true

  1. Document created in notepad is an example of:

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

Show Answer

a. Text file

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

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

Show Answer

b. Binary file

  1. 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

Show Answer

d. To read and write the contents o a binary file specific software is required

  1. 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

Show Answer

b. Garbage values

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

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

Show Answer

a. .py

  1. Which of the following extension does not represent a text file?

    a. .txt
    b. .docx
    c. .csv
    d. .py

Show Answer

b. docx

  1. ___________ can be read by humans.

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

Show Answer

a. Text files

  1. 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

Show Answer

c. Both (a) and (b)

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

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

Show Answer

d.

  1. 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

Show Answer

b. opens myfile.txt in read mode

  1. 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.

Show Answer

b. opens myfile.txt in text and read mode

  1. 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+’)

Show Answer

b. Myobject = open(‘exp.txt’, ‘wb+’)

  1. 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)

Show Answer

d. both (a) and (b)

  1. 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 ‘FileNotFoundError’
    d. All are true

Show Answer

c. if file does not exists, than produces ‘FileNotFoundError’

  1. 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.

Show Answer

c. It places cursor at the beginning of file.

  1. It breaks the link between file object and data file stored in disk

    a. EOF
    b. Close()
    c. Break
    d. Dump()

Show Answer

b. Close()

  1. 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

Show Answer

a. No difference as both are used to read and write content

  1. A .pdf file is

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

Show Answer

b. Binary file

  1. For writing to a file we can use

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

Show Answer

e. Both (a) and (c)

  1. Method which returns total number of characters after writing it to the text file

    a. Write()
    b. Writelines()
    c. Both
    d. None                    

Show Answer

a. Write()

  1. 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

Show Answer

a. No output

  1. 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

Show Answer

b. Fileobj.write(‘23’)

  1. 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

Show Answer

c.  Both (a) and (b)

  1. 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()

Show Answer

d. Flush()

  1. 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

Show Answer

d. All of the above

  1. 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’)

Show Answer

a. ‘techtipnow\n’

  1. 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()

Show Answer

b. Readline()

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

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

Show Answer

d. Both (a) and (c)

  1. Readlines() returns all the lines as a _____ of string.

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

Show Answer

a. List

  1. 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

Show Answer

b. techtip

  1. 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

Show Answer

d. No output

  1. ____ method is used to position the file object at a particular position in a data file

    a. Seek()
    b. Tell()
    c. Dump()
    d. Move()

Show Answer

a. Seek()

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

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

Show Answer

b. Load()

  1. ______ method is used to convert python objects for writing data in a binary file.

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

Show Answer

a. Dump()

  1. 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. Pickiling
    b. Serialization
    c. Dumping
    d. Both (a) and (b)

Show Answer

d. Both (a) and (b)

  1. To add and display data for a binary file we can use _________ module

    a. Pickle
    b. Serialize
    c. Pandas
    d. Panther

Show Answer

a. Pickle

  1. 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

Show Answer

d. Opens sample.txt in text, read and write mode

4 thoughts on “File Handling Python MCQ”

  1. Krishna Kumar Gupta

    Thank you, for providing contents.
    Some correction can be done in the answer of the following questions:
    [ 4, 6, 25, 30 ]
    Hoping for corrections,
    Also if mistakes are on my end please tell me with explanation of solutions of the questions

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!